|
|
|
@ -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<List<Integer>> levelOrder(TreeNode root) { |
|
|
|
|
List<List<Integer>> result = new ArrayList<>(); |
|
|
|
|
if (root == null) { |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
Deque<TreeNode> deque = new LinkedList<>(); |
|
|
|
|
deque.add(root); |
|
|
|
|
while (!deque.isEmpty()) { |
|
|
|
|
int l = deque.size(); |
|
|
|
|
List<Integer> 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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|