From a413e12a61d2bf8cf5b59ff03a180e14545b17a3 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 13 Jul 2020 09:41:17 +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 | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/二叉树相关.md b/blogs/Algorithm/剑指 Offer/二叉树相关.md index 0af2c92..b4e060e 100644 --- a/blogs/Algorithm/剑指 Offer/二叉树相关.md +++ b/blogs/Algorithm/剑指 Offer/二叉树相关.md @@ -292,3 +292,36 @@ class Solution { } ``` +#### [32 - II. 从上到下打印二叉树 II](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof/) + + +```java +class Solution { + + public List> levelOrder(TreeNode root) { + List> result = new ArrayList<>(); + if (root == null) { + return result; + } + Deque deque = new LinkedList<>(); + deque.add(root); + while (!deque.isEmpty()) { + int l = deque.size(); + List list = new ArrayList<>(); + for (int i = 0; i < l; i++) { + TreeNode node = deque.poll(); + list.add(node.val); + if (node.left != null) { + deque.add(node.left); + } + if (node.right != null) { + deque.add(node.right); + } + } + result.add(list); + } + return result; + } +} +``` +