-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path105.ConstructBinaryTreeFromPreorderAndInorderTraversal.java
More file actions
38 lines (30 loc) · 1.3 KB
/
105.ConstructBinaryTreeFromPreorderAndInorderTraversal.java
File metadata and controls
38 lines (30 loc) · 1.3 KB
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
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left
* = left; this.right = right; } }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
}
TreeNode build(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) {
if (preStart > preEnd) {
return null;
}
int rootValue = preorder[preStart];
TreeNode root = new TreeNode(rootValue);
int rootIndexOfInorder = -1;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootValue) {
rootIndexOfInorder = i;
break;
}
}
int LeftSize = rootIndexOfInorder - inStart;
// 做的时候,preOrderIndex因为例子的原因无法推出规则。
root.left = build(preorder, inorder, preStart + 1, preStart + LeftSize, inStart, rootIndexOfInorder - 1);
root.right = build(preorder, inorder, preStart + LeftSize + 1, preEnd, rootIndexOfInorder + 1, inEnd);
return root;
}
}