-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path105-buildTree.java
More file actions
44 lines (41 loc) · 1.55 KB
/
105-buildTree.java
File metadata and controls
44 lines (41 loc) · 1.55 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
39
40
41
42
43
44
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
// 通过中序和前序查找建立树
class Solution_105 {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0) return null;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<inorder.length; i++) {
map.put(inorder[i], i);
}
return build(map, 0, preorder.length-1, preorder, 0, inorder.length-1, inorder);
}
// 如果每个加上找pro就是 n方,通过hashmap解决了这个问题。
public TreeNode build(HashMap<Integer, Integer> map, int preLeft, int preRight, int[] preorder, int inLeft, int inRight, int[] inorder) {
if(preLeft > preRight || inLeft > inRight) {
return null;
}
int pro = preorder[preLeft];
int inIndex = inLeft;
inLeft = map.get(pro);
TreeNode root = new TreeNode(pro);
root.left = build(map, preLeft+1, preLeft+inLeft-inIndex, preorder, inIndex, inLeft-1,inorder);
root.right = build(map, preLeft+inLeft-inIndex+1, preRight, preorder, inLeft+1, inRight, inorder);
return root;
}
public static void main(String[] args) {
Solution_105 solu = new Solution_105();
int[] A = new int[]{3,9,20,15,7};
int[] B = new int[]{9,3,15,20,7};
TreeNode s= solu.buildTree(A, B);
System.out.println(s);
}
}