Construct Binary Tree from Inorder and Postorder Traversal LeetCode solution in C++

1
class Solution {
2
public:
3
TreeNode *buildTree(vector<int> &ino, vector<int> &post) {
4
int i1 = post.size() - 1;
5
return solve(i1, ino, post, 0, ino.size() - 1);
6
}
7
TreeNode *solve(int &i, vector<int> &in, vector<int> &post, int l, int r) {
8
if (l > r) return NULL;
9
int x = r;
10
while (post[i] != in[x]) {
11
x--;
12
}
13
i--;
14
// cout<<in[x]<<" ";
15
TreeNode *root = new TreeNode(in[x]);
16
root->right = solve(i, in, post, x + 1, r);
17
root->left = solve(i, in, post, l, x - 1);
18
return root;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0