-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (28 loc) · 1007 Bytes
/
Solution.java
File metadata and controls
33 lines (28 loc) · 1007 Bytes
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
/**
* 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[] preorder, int[] inorder) {
return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
public TreeNode build(int[] preorder, int preBeg, int preEnd, int[] inorder, int inBeg, int inEnd) {
if (inBeg > inEnd) return null;
TreeNode root = new TreeNode(preorder[preBeg]);
int pos = -1;
for (int i = inBeg; i <= inEnd; i++) {
if (preorder[preBeg] == inorder[i]) {
pos = i;
break;
}
}
root.left = build(preorder, preBeg + 1, preBeg + pos - inBeg, inorder, inBeg, pos - 1);
root.right = build(preorder, preBeg + pos - inBeg + 1, preEnd, inorder, pos + 1, inEnd);
return root;
}
}