-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105_ConstructBinaryTreeFromPreorderAndInorderTraversal.java
62 lines (53 loc) · 1.91 KB
/
105_ConstructBinaryTreeFromPreorderAndInorderTraversal.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
57
58
59
60
61
62
/*
* Given preorder and inorder traversal of a tree, construct the binary tree.
* Note:
* You may assume that duplicates do not exist in the tree.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helper(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
}
public TreeNode helper(int[] preorder, int[] inorder, int prePos, int preEnd, int inPos, int inEnd) {
if(inPos > inEnd || prePos > preEnd) return null;
TreeNode cur = new TreeNode(preorder[prePos]);
int index = 0;
for(int i = inPos; i<= inEnd; i++) {
if(inorder[i] == cur.val) {
index = i;
break;
}
}
cur.left = helper(preorder, inorder, prePos + 1, prePos + index - inPos, inPos, index - 1);
cur.right = helper(preorder, inorder, prePos + index - inPos + 1, preEnd, index + 1, inEnd);
return cur;
}
}
//
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1);
}
private TreeNode buildTree(int[] preorder, int pstart, int pend, int[] inorder, int istart, int iend) {
if (pstart>pend)
return null;
for (int i=istart; i<=iend; i++) {
if (inorder[i]==preorder[pstart]) {
TreeNode root = new TreeNode(inorder[i]);
root.left = buildTree(preorder, pstart+1, pstart+1+(i-1-istart), inorder, istart, i-1);
root.right = buildTree(preorder, pend-(iend-i-1), pend, inorder, i+1, iend);
return root;
}
}
return null;
}
}