-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution32.java
48 lines (46 loc) · 1.21 KB
/
Solution32.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
package sword_offer;
// page 171 从上到下打印二叉树
import java.util.LinkedList;
import java.util.Queue;
public class Solution32 {
public static void printTreeTopToBottom(BinaryTreeNode root) {
if (root == null)
return;
// 创建个队列queue
Queue<BinaryTreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
// 弹出后打印,再插入该节点的左节点、右节点
BinaryTreeNode node = queue.poll();
System.out.println(node.value);
if (node.leftNode != null)
queue.add(node.leftNode);
if (node.rightNode != null)
queue.add(node.rightNode);
}
}
// 测试
public static void main(String[] args) {
BinaryTreeNode a = new BinaryTreeNode();
BinaryTreeNode b = new BinaryTreeNode();
BinaryTreeNode c = new BinaryTreeNode();
BinaryTreeNode d = new BinaryTreeNode();
BinaryTreeNode e = new BinaryTreeNode();
BinaryTreeNode f = new BinaryTreeNode();
BinaryTreeNode g = new BinaryTreeNode();
a.leftNode = b;
a.rightNode = c;
b.leftNode = d;
b.rightNode = e;
c.leftNode = f;
c.rightNode = g;
a.value = 8;
b.value = 6;
c.value = 10;
d.value = 5;
e.value = 7;
f.value = 9;
g.value = 11;
printTreeTopToBottom(a);
}
}