forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0106.cpp
More file actions
26 lines (17 loc) · 673 Bytes
/
0106.cpp
File metadata and controls
26 lines (17 loc) · 673 Bytes
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
class Solution {
public:
TreeNode* postdfs(vector<int>& in, vector<int>& post, TreeNode* stop) {
if ( post.empty() || (stop && in.back() == stop->val) )
return NULL;
TreeNode* root = new TreeNode(post.back());
post.pop_back();
root -> right = postdfs(in, post, root);
in.pop_back();
root -> left = postdfs(in, post, stop);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
vector<int> in(inorder), post(postorder);
return postdfs(in, post, (TreeNode*)NULL);
}
};