-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrintFromTopToBottom.java
74 lines (64 loc) · 2.2 KB
/
PrintFromTopToBottom.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
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
/**
* Created by Yang on 2017/4/17.
* 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
*/
public class PrintFromTopToBottom {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
public String toString() {
if(left != null && right != null) {
return "[" + left + "," + val + "," + right + "]";
} else if(left != null) {
return "[" + left + "," + val + ",#,]";
} else if(right != null) {
return "[#," + val + "," + right + "]";
} else {
return "" + val;
}
}
}
public ArrayList<Integer> printFromTopToBottom(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
if(root == null) {
return res;
}
Deque<TreeNode> nodes = new LinkedList<>();
nodes.offer(root);
while(!nodes.isEmpty()) {
TreeNode node = nodes.poll();
res.add(node.val);
if(node.left != null) {
nodes.offer(node.left);
}
if(node.right != null) {
nodes.offer(node.right);
}
}
return res;
}
public static void main(String[] args) {
PrintFromTopToBottom printFromTopToBottom = new PrintFromTopToBottom();
TreeNode tree = new TreeNode(8);
tree.left = new TreeNode(6);
tree.right = new TreeNode(10);
tree.left.left = new TreeNode(5);
tree.left.right = new TreeNode(7);
tree.right.left = new TreeNode(9);
tree.right.right = new TreeNode(11);
//[8, 6, 10, 5, 7, 9, 11]
System.out.println(printFromTopToBottom.printFromTopToBottom(tree));
tree.left = null;
//[8, 10, 9, 11]
System.out.println(printFromTopToBottom.printFromTopToBottom(tree));
tree.right = null;
//[8]
System.out.println(printFromTopToBottom.printFromTopToBottom(tree));
System.out.println(printFromTopToBottom.printFromTopToBottom(null));
}
}