-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_106.java
More file actions
50 lines (44 loc) · 1.62 KB
/
Solution_106.java
File metadata and controls
50 lines (44 loc) · 1.62 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
45
46
47
48
49
50
/*
106. 从中序与后序遍历序列构造二叉树\
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
*/
/*
普通做法:分治
*/
class Solution {
public static void main(String[] args) {
int[] inorder = new int[]{9,3,15,20,7};
int[] postorder = new int[]{9,15,7,20,3};
Solution solution = new Solution();
TreeNode answer = solution.buildTree(inorder, postorder);
System.out.println(answer.val);
System.out.println(answer.left.val+" "+answer.right.val);
System.out.println(answer.right.left.val+" "+answer.right.right.val);
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
if ( postorder.length == 0 || inorder.length == 0 ) return null;
if ( inorder.length == 1 ) return new TreeNode(postorder[0]);
return solve(inorder, postorder, 0, 0, postorder.length);
}
private TreeNode solve(int[] inorder, int[] postorder, int inbegin, int postbegin, int length) {
if ( length == 0 ) return null;
TreeNode root = new TreeNode(postorder[postbegin+length-1]);
//i是根节点的位置
int i = 0;
while ( postorder[postbegin+length-1] != inorder[inbegin+i] ) i++;
root.left = solve(inorder, postorder,inbegin, postbegin, i);
root.right = solve(inorder, postorder, inbegin + i + 1, postbegin + i,length - i - 1);
return root;
}
}