forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT22.java
More file actions
31 lines (27 loc) · 833 Bytes
/
T22.java
File metadata and controls
31 lines (27 loc) · 833 Bytes
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
package web; /**
* @program LeetNiu
* @description: 从上往下打印二叉树
* @author: mf
* @create: 2020/01/13 10:23
*/
import java.util.ArrayList;
import java.util.LinkedList;
/**
* 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
*/
public class T22 {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
if (root == null) return list;
// 队列
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
return list;
}
}