-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution105.java
42 lines (35 loc) · 1.08 KB
/
Solution105.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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 从前序与中序遍历序列构造二叉树
* @date: 2019/03/06
*/
public class Solution105 {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return dfs(preorder, inorder, 0, 0, inorder.length - 1);
}
private TreeNode dfs(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) {
if (inStart > inEnd || preStart > preorder.length - 1) {
return null;
}
TreeNode node = new TreeNode(preorder[preStart]);
int inIndex = 0;
for (int i = inStart; i <= inEnd; ++i) {
if (inorder[i] == node.val) {
inIndex = i;
break;
}
}
node.left = dfs(preorder, inorder, preStart + 1, inStart, inIndex - 1);
node.right = dfs(preorder, inorder, preStart + inIndex - inStart + 1, inIndex + 1, inEnd);
return node;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}