-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructBinaryTreeFromInorderAndPostorder.cpp
More file actions
61 lines (46 loc) · 1.98 KB
/
Copy pathConstructBinaryTreeFromInorderAndPostorder.cpp
File metadata and controls
61 lines (46 loc) · 1.98 KB
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
58
59
60
61
// Q126 https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
// Time: O(n)
// Space: O(n)
class Solution {
public:
TreeNode* helper(vector<int> &post,int ps,int pe,
vector<int> &in,int is,int ie,
map<int,int> &m){
if(ps>pe||is>ie) return NULL;
TreeNode* root=new TreeNode(post[pe]);
int inroot=m[root->val];
int numsleft=inroot-is;
root->left=helper(post,ps,ps+numsleft-1,in,is,inroot-1,m);
root->right=helper(post,ps+numsleft,pe-1,in,inroot+1,ie,m);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
int psize=postorder.size(), isize=inorder.size();
// if(psize==0||isize==0) return NULL;
map<int,int> m;
for(int i=0;i<isize;i++)
m[inorder[i]]=i;
TreeNode* root=helper(postorder,0,psize-1,inorder,0,isize-1,m);
return root;
}
};
// https://classroom.codingninjas.com/app/classroom/me/6855/content/92019/offering/1027889/problem/355
//Given array (not vectors) and not using hashmap
BinaryTreeNode<int>* buildTree(int *postOrder, int postLength, int *inOrder, int inLength) {
BinaryTreeNode<int> *root = new BinaryTreeNode<int>(postOrder[postLength-1]);
if(postLength == 1) // base case when only root is present and no left or right node
return root;
if(postLength == 0) //if one of both right or left is present check for 2, 1 2, 1 2;;;dry run it
return NULL;
int i = 0;
while(inOrder[i] != postOrder[postLength - 1]){
i++;
}
int left_size = i;
int right_size = inLength -i - 1;
BinaryTreeNode<int> *leftSubtree = buildTree(postOrder, left_size, inOrder, left_size);
BinaryTreeNode<int> *rightSubtree = buildTree(postOrder + left_size, right_size, inOrder + 1 + left_size, right_size);
root -> left = leftSubtree;
root -> right = rightSubtree;
return root;
}