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