-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeFromInOrder&PostOrder.java
66 lines (53 loc) · 1.47 KB
/
BinaryTreeFromInOrder&PostOrder.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
56
57
58
59
60
61
62
63
64
65
66
/*
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
*/
/**
* 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;
* }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildBT(inorder,postorder,0,inorder.length-1,0,postorder.length-1);
}
private TreeNode buildBT(int[] inorder,int[] postorder,int ISI,int IEI,int PSI,int PEI){
if(PSI > PEI){
return null;
}
TreeNode root = new TreeNode(postorder[PEI]);
if(PSI == PEI){
return root;
}
int i = ISI;
for(i = ISI; i <= IEI; i++){
if(inorder[i] == root.val){
break;
}
}
int count = (i - ISI);
root.left = buildBT(inorder,postorder,ISI,i-1,PSI,PSI+count-1);
root.right = buildBT(inorder,postorder,i+1,IEI,PSI+count,PEI-1);
return root;
}
}