AcWing 43. 不分行从上往下打印二叉树
原题链接
简单
作者:
小轩喵灬
,
2025-01-11 17:20:10
,
所有人可见
,
阅读 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> printFromTopToBottom(TreeNode root) {
Deque<TreeNode> queue =new LinkedList<>();
queue.add(root);
List<Integer> res = new ArrayList<>();
while(!queue.isEmpty()) {
TreeNode poll = queue.pollFirst();
if (poll == null) {
continue;
}
res.add(poll.val);
queue.offer(poll.left);
queue.offer(poll.right);
}
return res;
}
}