forked from algorithm020/algorithm020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_buildTree.java
More file actions
37 lines (35 loc) · 1.35 KB
/
Solution_buildTree.java
File metadata and controls
37 lines (35 loc) · 1.35 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
//优化
HashMap<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
//自顶向下编程
return appendChild(0,preorder.length,preorder,0,inorder.length,inorder);
}
private TreeNode appendChild(int preStart,int preEnd,int[] preorder, int inStart,int inEnd,int[] inorder){
//其实就是给定一个节点,追加左右节点即可
//如果左右节点都找不到,就退出循环
if(preStart==preEnd){
return null;
}
//前序遍历第一个节点就是根节点,即preStart
TreeNode root=new TreeNode(preorder[preStart]);
//找到中序遍历中该根节点所处位置,用于划分出左、右子树
int rootIndex=map.get(preorder[preStart]);
//下沉到下一层
root.left=appendChild(preStart+1,preStart+rootIndex-inStart+1,preorder,inStart,rootIndex,inorder);
root.right=appendChild(preStart+rootIndex-inStart+1,preEnd,preorder,rootIndex+1,inEnd,inorder);
return root;
}
}