-
Notifications
You must be signed in to change notification settings - Fork 0
/
Construct Binary Tree from Preorder and Inorder Traversal.java
55 lines (43 loc) · 1.43 KB
/
Construct Binary Tree from Preorder and Inorder 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
/*
Given preorder and inorder traversal of a tree, construct the binary tree.
Link: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
Example: None
Solution: None
Source: None
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int preStart = 0;
int preEnd = preorder.length - 1;
int inStart = 0;
int inEnd = inorder.length - 1;
return construct(preorder, preStart, preEnd, inorder, inStart, inEnd);
}
public TreeNode construct(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd){
if (preStart > preEnd || inStart > inEnd){
return null;
}
int val = preorder[preStart];
TreeNode p = new TreeNode(val);
//find parent element index from inorder
int k = 0;
for (int i = 0; i < inorder.length; i++){
if (val == inorder[i]){
k = i;
break;
}
}
p.left = construct(preorder, preStart + 1, preStart + (k - inStart), inorder, inStart, k - 1);
p.right = construct(preorder, preStart + (k - inStart) + 1, preEnd, inorder, k + 1 , inEnd);
return p;
}
}