-
Notifications
You must be signed in to change notification settings - Fork 1
/
1028.cpp
57 lines (54 loc) · 1.5 KB
/
1028.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include<bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* build_tree(queue<pair<int, int>> &q, int depth) {
if (q.empty() || q.front().second != depth) return nullptr;
TreeNode* r = new TreeNode(q.front().first);
q.pop();
r->left = build_tree(q, depth+1);
r->right = build_tree(q, depth+1);
return r;
}
/**
* 执行用时 :24 ms, 在所有 C++ 提交中击败了89.80%的用户
* 内存消耗 :21.2 MB, 在所有 C++ 提交中击败了100.00%的用户
*/
TreeNode* recoverFromPreorder(string S) {
queue<pair<int, int>> q;
S.push_back('-'); // 添加一个方便处理
int count = 0, left = 0;
for (int i = 0; i < S.size(); ++i) {
if (S[i] == '-') {
if (left != i) {
q.push(pair<int, int>(atoi(S.substr(left, i - left).c_str()), count));
count = 0;
}
++count;
left = i+1;
}
}
return build_tree(q, 0);
}
};
int main(int argc, char const *argv[])
{
Solution s;
s.recoverFromPreorder("1-401--349---90--88");
return 0;
}