-
Notifications
You must be signed in to change notification settings - Fork 2
/
CreateTree.java
72 lines (64 loc) · 2.2 KB
/
CreateTree.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
package JZOffer2;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class CreateTree {
public static void main(String[] args) {
TreeNode root = create();//建树
Main34 main = new Main34();
main.pathSum(root, 22);
preOrder(root);
show(root);//前序遍历树
}
private static void show(TreeNode root){
if (root == null) return;
System.out.print(root.val+" ");
show(root.left);
show(root.right);
}
static String preOrder(TreeNode root){
StringBuffer sb = new StringBuffer();
LinkedList<TreeNode> stack = new LinkedList<>();
if(root == null){
return sb.toString();
}
stack.push(root);
while(!stack.isEmpty()){
TreeNode current = stack.pop();
if(current != null){
sb.append(current.val);
stack.push(current.left);
stack.push(current.right);
}
}
return sb.toString();
}
private static TreeNode create() {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String substring = str.substring(1, str.length()-1);
String[] strs = substring.split(",");//获取输入的字符串序列
if (strs.length == 0) return null;
Queue<TreeNode> queue = new LinkedList<>();
TreeNode root = new TreeNode(Integer.parseInt(strs[0]));
queue.offer(root);
int i=1;//记录序列遍历数量
while (i<strs.length){
int size = queue.size();//记录当前层次的节点数量
for (int j = 0;j<size;j++){
TreeNode t = queue.poll();
if (i<strs.length&&!strs[i].equals("null")){
t.left = new TreeNode(Integer.parseInt(strs[i]));
queue.offer(t.left);//非空子树入队
}
i++;
if (i<strs.length&&!strs[i].equals("null")){
t.right = new TreeNode(Integer.parseInt(strs[i]));
queue.offer(t.right);
}
i++;
}
}
return root;
}
}