From 13a8614406e35cb9dff26ce9a62455c3f5fe2fe1 Mon Sep 17 00:00:00 2001 From: CyC2018 Date: Wed, 28 Oct 2026 13:45:04 +0800 Subject: [PATCH] =?UTF-8?q?Update=2032.1=20=E4=BB=8E=E4=B8=8A=E5=BE=80?= =?UTF-8?q?=E4=B8=8B=E6=89=93=E5=8D=B0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。 --- notes/32.1 从上往下打印二叉树.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/notes/32.1 从上往下打印二叉树.md b/notes/32.1 从上往下打印二叉树.md index da0d6d8b..ea9f49a1 100644 --- a/notes/32.1 从上往下打印二叉树.md +++ b/notes/32.1 从上往下打印二叉树.md @@ -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 PrintFromTopToBottom(TreeNode root) { +public int[] decorateRecord(TreeNode root) { + if (root == null) { + return new int[0]; + } Queue queue = new LinkedList<>(); ArrayList ret = new ArrayList<>(); queue.add(root); @@ -32,6 +35,10 @@ public ArrayList 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; } ```