题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
层级遍历即可
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
*/
public class _023_PrintFromTopToBottom {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
// 写一个Queue,作为层级遍历的容器
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (queue.size() != 0) {
int size = queue.size();
// 将上一层的数据都取出来
while (size-- != 0) {
TreeNode tempNode = queue.remove();
res.add(tempNode.val);
if (tempNode.left != null) {
queue.add(tempNode.left);
}
if (tempNode.right != null) {
queue.add(tempNode.right);
}
}
}
return res;
}
}
/**
* class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* <p>
* public TreeNode(int val) {
* this.val = val;
* <p>
* }
* <p>
* }
*/