-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrintTreeFromTopToBottom.java
45 lines (41 loc) · 1.16 KB
/
PrintTreeFromTopToBottom.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
package offer.problem32;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created with IntelliJ IDEA
*
* @Author yuanhaoyue swithaoy@gmail.com
* @Description 32.1. 从上往下打印二叉树(层次遍历二叉树)
* @Date 2019-01-24
* @Time 20:49
*/
public class PrintTreeFromTopToBottom {
public ArrayList<Integer> printFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root != null) {
//使用队列辅助运算
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
return list;
}
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
}