-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeInorderPostOrder.java
More file actions
42 lines (38 loc) · 1.53 KB
/
BinaryTreeInorderPostOrder.java
File metadata and controls
42 lines (38 loc) · 1.53 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
public class BinaryTreeInorderPostOrder {
public TreeNode buildTree(int[] inorder, int[] postorder) {
int lin = inorder.length;
int lpost = postorder.length;
if (lin != lpost || lin == 0 || lpost == 0) {
return null;
}
return recur(inorder, postorder, 0, lin - 1, 0, lpost - 1);
}
public TreeNode recur(int[] inorder, int[] postorder, int instart, int inend, int poststart, int postend) {
//System.out.println("instart = " + instart + "; inend = " + inend + "; poststart = " + poststart + "; postend = " + postend);
if (inend == instart || poststart == postend) {
//System.out.println("return: " + instart + " " + inend + " " + poststart + " " + postend);
return new TreeNode(inorder[instart]);
}
if (instart < 0 || instart >= inorder.length || inend < 0 || inend >= inorder.length
|| poststart < 0 || poststart >= postorder.length || postend < 0 || postend >= postorder.length
||instart > inend || poststart > postend) {
return null;
}
TreeNode root = new TreeNode(postorder[postend]);
int in_root = 0;
for (int i = instart; i <= inend; i++) {
if (inorder[i] == root.val) {
in_root = i;
break;
}
}
root.left = recur(inorder, postorder, instart, in_root - 1, poststart, poststart + in_root - 1 - instart);
root.right = recur(inorder, postorder, in_root + 1, inend, postend - 1 - (inend - in_root - 1), postend - 1);
return root;
}
public void testBuild() {
int[] in = {2,1};
int[] post = {2,1};
TreeNode root = buildTree(in, post);
}
}