字符串转二叉树 leetcode C++ 实现
2021-07-14 22:06
标签:trim erase amp pac auto int length root line 字符串转二叉树 leetcode C++ 实现 标签:trim erase amp pac auto int length root line 原文地址:https://www.cnblogs.com/theodoric008/p/9537607.htmlstruct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}
void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}
TreeNode* stringToTreeNode(string input) {
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
if (input.empty()) {
return nullptr;
}
string item;
stringstream ss;
ss.str(input);
getline(ss, item, ‘,‘);
auto root = new TreeNode(stoi(item));
queue
上一篇:python基础学习-字符串
下一篇:Python装饰器