Update 32.1 从上往下打印二叉树.md

题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
CyC2018
2026-10-28 13:45:04 +08:00
parent 92244572f0
commit 13a8614406
+10 -3
View File
@@ -1,6 +1,6 @@
# 32.1 从上往下打印二叉树
[NowCoder](https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tqId=11175&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
[Leetcode](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/)
## 题目描述
@@ -17,7 +17,10 @@
不需要使用两个队列分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。
```java
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
public int[] decorateRecord(TreeNode root) {
if (root == null) {
return new int[0];
}
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> ret = new ArrayList<>();
queue.add(root);
@@ -32,6 +35,10 @@ public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
queue.add(t.right);
}
}
return ret;
int[] res = new int[ret.size()];
for (int i = 0; i < ret.size(); i++) {
res[i] = ret.get(i);
}
return res;
}
```