-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildTreeInPo.java
More file actions
19 lines (18 loc) · 823 Bytes
/
BuildTreeInPo.java
File metadata and controls
19 lines (18 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;
import java.util.Stack;
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length ==0 && postorder.length == 0) return null;
TreeNode root = new TreeNode(postorder[postorder.length -1]);
int i = 0;
for(;i < inorder.length; i++){
if(inorder[i] == root.val) break;
}
int[] subInorder = Arrays.copyOfRange(inorder, i + 1, inorder.length);
int[] subPostorder = Arrays.copyOfRange(postorder,inorder.length -1 - subInorder.length,postorder.length-1);
root.right = buildTree(subInorder,subPostorder);
int[] lSubInorder = Arrays.copyOfRange(inorder,0,i);
root.left = buildTree(lSubInorder, Arrays.copyOfRange(postorder,0,lSubInorder.length));
return root;
}
}