forked from carpeventus/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintTreeEveryLayer.java
More file actions
73 lines (64 loc) · 1.96 KB
/
Copy pathPrintTreeEveryLayer.java
File metadata and controls
73 lines (64 loc) · 1.96 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Chap4;
import java.util.LinkedList;
import java.util.Queue;
/**
* 逐层打印二叉树,即打印完一层后要有换行符
*/
public class PrintTreeEveryLayer {
private class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public void printEveryLayer2(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
// 下一层的结点数
int nextLevel = 0;
// 本层还有多少结点未打印
int toBePrinted = 1;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
System.out.print(node.val + " ");
// 每打印一个,减1
toBePrinted--;
// 每一个元素加入队列,加1
if (node.left != null) {
queue.offer(node.left);
nextLevel++;
}
if (node.right != null) {
queue.offer(node.right);
nextLevel++;
}
// 本层打印完毕
if (toBePrinted == 0) {
System.out.println();
toBePrinted = nextLevel;
nextLevel = 0;
}
}
}
// 也是分行打印,比上面简洁
public void printEveryLayer(TreeNode root) {
if (root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int layerSize = queue.size();
for (int i = 0; i < layerSize; i++) {
TreeNode node = queue.poll();
System.out.println(node.val+" ");
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
System.out.println();
}
}
}