forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructBinaryTreeFromInorderAndPostorderTraversal.java
55 lines (44 loc) · 1.34 KB
/
ConstructBinaryTreeFromInorderAndPostorderTraversal.java
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
// TC : O(n)
// SC : O(n)
class Solution {
private int pos =0;
public TreeNode buildTree(int[] inorder, int[] postorder) {
pos = postorder.length-1;
Map<Integer, Integer> map = new HashMap<Integer,Integer>();
for (int i=0;i<inorder.length;++i) {
map.put(inorder[i], i);
}
TreeNode root = constructTree(inorder, postorder, 0, inorder.length-1, map );
return root;
}
private TreeNode constructTree(int[] inorder, int[] postorder, int startIn, int endIn, Map<Integer, Integer> map){
if(pos<0 || startIn> endIn){
return null;
}
int val = postorder[pos];
TreeNode node = new TreeNode(val);
pos--;
// indentify the position in the inorder tree
int i= map.get(val);
TreeNode right = constructTree(inorder, postorder, i+1, endIn, map);
TreeNode left = constructTree(inorder, postorder, startIn, i-1, map);
node.right = right;
node.left = left;
return node;
}
}