-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructBinaryTreeFromInorderAndPostorderTraversal.java
46 lines (42 loc) · 1.49 KB
/
ConstructBinaryTreeFromInorderAndPostorderTraversal.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
package leetcode;
/*
* Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class ConstructBinaryTreeFromInorderAndPostorderTraversal {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder == null || inorder.length == 0 || postorder == null || postorder.length == 0) {
return null;
}
if (inorder.length == 1) {
return new TreeNode(inorder[0]);
}
TreeNode node = new TreeNode(postorder[postorder.length - 1]);
int index = indexOf(inorder, node.val);
if (index >= 1) {
node.left = buildTree(Arrays.copyOfRange(inorder, 0, index), Arrays.copyOfRange(postorder, 0, index));
}
if (postorder.length -1 >= index)
node.right = buildTree(Arrays.copyOfRange(inorder, index + 1, inorder.length), Arrays.copyOfRange(postorder, index, postorder.length - 1));
return node;
}
public int indexOf(int[] arr, int val) {
if (arr == null || arr.length == 0) return -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == val) {
return i;
}
}
return -1;
}
}