From 68616209ea54f9dc268c583368ca7ff9c855d20f Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Tue, 7 Jul 2020 09:01:59 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9B=B8?= =?UTF-8?q?=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Algorithm/剑指 Offer/二叉树相关.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/二叉树相关.md b/blogs/Algorithm/剑指 Offer/二叉树相关.md index 192bd39..6e5a0a3 100644 --- a/blogs/Algorithm/剑指 Offer/二叉树相关.md +++ b/blogs/Algorithm/剑指 Offer/二叉树相关.md @@ -133,3 +133,35 @@ class Solution { } ``` +#### [32 - I. 从上到下打印二叉树](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/) + +```java +class Solution { + + public int[] levelOrder(TreeNode root) { + if (root == null) { + return new int[0]; + } + Queue queue = new LinkedList<>(); + queue.add(root); + List list = new ArrayList<>(); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + list.add(node.val); + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + } + int[] result = new int[list.size()]; + int index = 0; + for (int i : list) { + result[index++] = i; + } + return result; + } +} +``` +