-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path106.java
28 lines (28 loc) · 915 Bytes
/
106.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree(inorder,postorder,0,inorder.length-1,postorder.length-1);
}
private TreeNode buildTree(int[] inorder,int[] postorder,int istart,int iend,int ppos){
if(ppos>=postorder.length||istart>iend) return null;
TreeNode root = new TreeNode(postorder[ppos]);
int icur = 0;
for(int i=istart;i<=iend;i++){
if(inorder[i]==postorder[ppos]){
icur = i;
break;
}
}
root.left = buildTree(inorder,postorder,istart,icur-1,ppos-1-(iend-icur));
root.right = buildTree(inorder,postorder,icur+1,iend,ppos-1);
return root;
}
}