-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecreateTree.java
89 lines (74 loc) · 2.48 KB
/
RecreateTree.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.ldd;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class RecreateTree {
public static TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || preorder.length == 0) {
return null;
}
Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
int length = preorder.length;
for (int i = 0; i < length; i++) {
indexMap.put(inorder[i], i);
}
TreeNode root = buildTree(preorder, 0, length - 1, inorder, 0, length - 1, indexMap);
return root;
}
public static TreeNode buildTree(int[] preorder, int preorderStart, int preorderEnd, int[] inorder,
int inorderStart, int inorderEnd, Map<Integer, Integer> indexMap) {
if (preorderStart > preorderEnd) {
return null;
}
int rootVal = preorder[preorderStart];
TreeNode root = new TreeNode(rootVal);
if (preorderStart == preorderEnd) {
return root;
} else {
int rootIndex = indexMap.get(rootVal);
int leftNodes = rootIndex - inorderStart;
int rightNodes = inorderEnd - rootIndex;
TreeNode leftSubTree = buildTree(preorder, preorderStart + 1, preorderStart + leftNodes, inorder,
inorderStart, rootIndex - 1, indexMap);
TreeNode rightSubTree = buildTree(preorder, preorderEnd - rightNodes + 1, preorderEnd, inorder,
rootIndex + 1, inorderEnd, indexMap);
root.left = leftSubTree;
root.right = rightSubTree;
return root;
}
}
public static TreeNode buildStackTree(int[] preorder, int[] inorder) {
if (preorder == null || preorder.length == 0) {
return null;
}
TreeNode root = new TreeNode(preorder[0]);
int length = preorder.length;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
int inorderIndex = 0;
for (int i = 1; i < length; i++) {
int preorderVal = preorder[i];
TreeNode node = stack.peek();
if (node.val != inorder[inorderIndex]) {
node.left = new TreeNode(preorderVal);
stack.push(node.left);
} else {
if (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {
stack.pop();
inorderIndex++;
}
node.right = new TreeNode(preorderVal);
stack.push(node.right);
}
}
return root;
}
public static void main(String[] args) {
int[] preorder = { 3, 9, 20, 15, 7 };
int[] inorder = { 9, 3, 15, 20, 7 };
// TreeNode root = buildTree(preorder, inorder);
TreeNode root = buildStackTree(preorder, inorder);
System.out.println(InOrderTraversal.preOrderTraversal(root));
System.out.println(InOrderTraversal.inOrderTraversal(root));
}
}