-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildTree.java
More file actions
53 lines (47 loc) · 1.57 KB
/
buildTree.java
File metadata and controls
53 lines (47 loc) · 1.57 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
51
52
53
/**
* Author : WindAsMe
* File : buildTree.java
* Time : Create on 18-9-2
* Location : ../Home/JavaForLeeCode2/buildTree.java
* Function :
*/
public class buildTree {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
private static TreeNode buildTreeResult(int[] preorder, int[] inorder) {
if (preorder.length != inorder.length || preorder.length == 0)
return null;
return build(0, 0, inorder.length - 1, preorder, inorder);
}
private static TreeNode build(int prestart,int instart,int inend,int[] preorder,int[] inorder){
if(prestart > preorder.length - 1 || instart > inend)
return null;
TreeNode root = new TreeNode(preorder[prestart]);
int inindex = 0;
for (int i = instart; i <= inend; i++){
if(inorder[i] == root.val){
inindex = i;
break;
}
}
root.left = build(prestart + 1, instart, inindex - 1, preorder, inorder);
root.right = build(prestart + inindex - instart + 1, inindex + 1, inend, preorder, inorder);
return root;
}
private static void dfs(TreeNode node) {
if (node != null) {
System.out.print(node.val + " ");
dfs(node.left);
dfs(node.right);
}
}
public static void main(String[] args) {
int[] preorder = {3,9,20,15,7};
int[] inorder = {9,3,15,20,7};
dfs(buildTreeResult(preorder, inorder));
}
}