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; } ```