-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Recover-a-Tree-From-Preorder-Traversal.cpp
57 lines (48 loc) · 1.67 KB
/
Recover-a-Tree-From-Preorder-Traversal.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() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* recoverFromPreorder(string s) {
int i = 0;
int n = s.size();
vector<pair<int, int>> vp;
while (i < n) {
int cnt = 0;
while (i < n && s[i] == '-') {
++cnt;
++i;
}
string num;
while (i < n && '0' <= s[i] && s[i] <= '9') {
num += s[i];
++i;
}
int m = stoi(num);
vp.push_back({cnt, m});
}
map<int, pair<int, TreeNode*>> last;
TreeNode * result = new TreeNode(vp[0].second);
last[vp[0].first] = {0, result};
for (int i = 1; i < vp.size(); ++i) {
if (last[vp[i].first - 1].first == 0) {
last[vp[i].first - 1].second->left = new TreeNode(vp[i].second);
last[vp[i].first - 1].first = 1;
last[vp[i].first] = {0, last[vp[i].first - 1].second->left};
} else {
last[vp[i].first - 1].second->right = new TreeNode(vp[i].second);
last[vp[i].first - 1].first = 2;
last[vp[i].first] = {0, last[vp[i].first - 1].second->right};
}
}
return result;
}
};