Update 二叉树相关.md

master
Omooo 4 years ago
parent 9a5dae5dfd
commit a413e12a61
  1. 33
      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<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;
}
}
```

Loading…
Cancel
Save