-
Notifications
You must be signed in to change notification settings - Fork 0
/
Construct Binary Tree from Inorder and Postorder Traversal.java
56 lines (51 loc) · 1.49 KB
/
Construct Binary Tree from Inorder and Postorder Traversal.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
/*
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
time = O(n)
space = O(height) = O(n)
*/
/**
* 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) {
if (inorder == null || inorder.length == 0 || postorder == null || postorder.length == 0) {
return null;
}
return helper(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
}
private TreeNode helper(int[] in, int inStart, int inEnd, int[] post, int postStart, int postEnd) {
if (inStart > inEnd || postStart > postEnd) {
return null;
}
int rootVal = post[postEnd];
int index = 0;
for (int i = inStart; i <= inEnd; i++) {
if (in[i] == rootVal) {
index = i;
break;
}
}
int len = index - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = helper(in, inStart, index - 1, post, postStart, postStart + len - 1);
root.right = helper(in, index + 1, inEnd, post, postStart + len, postEnd - 1);
return root;
}
}