Skip to content

Commit 811ad31

Browse files
committed
feat: add 107
1 parent 56a799d commit 811ad31

File tree

5 files changed

+215
-40
lines changed

5 files changed

+215
-40
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
|83|[Remove Duplicates from Sorted List][083]|Linked List|
2828
|88|[Merge Sorted Array][088]|Array, Two Pointers|
2929
|100|[Same Tree][100]|Tree, Depth-first Search|
30+
|101|[Symmetric Tree][101]|Tree, Depth-first Search, Breadth-first Search|
31+
|104|[Maximum Depth of Binary Tree][104]|Tree, Depth-first Search|
32+
|107|[Binary Tree Level Order Traversal II][107]|Tree, Breadth-first Search|
3033

3134

3235
## Medium
@@ -71,6 +74,9 @@
7174
[083]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/083/README.md
7275
[088]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/088/README.md
7376
[100]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/100/README.md
77+
[101]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/101/README.md
78+
[104]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/104/README.md
79+
[107]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/107/README.md
7480

7581
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
7682
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/107/README.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [Binary Tree Level Order Traversal II][title]
2+
3+
## Description
4+
5+
Given a binary tree, return the *bottom-up level order* traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
6+
7+
For example:
8+
Given binary tree `[3,9,20,null,null,15,7]`,
9+
10+
```
11+
3
12+
/ \
13+
9 20
14+
/ \
15+
15 7
16+
```
17+
18+
return its bottom-up level order traversal as:
19+
20+
```
21+
[
22+
[15,7],
23+
[9,20],
24+
[3]
25+
]
26+
```
27+
28+
**Tags:** Tree, Breadth-first Search
29+
30+
31+
## 思路
32+
33+
题意是从下往上按层遍历二叉树,每一层是从左到右,由于是从下往上,那么我们就先遍历,当链表尺寸小于深度的时候,我们在链表首部插入新的节点,然后在最后遍历完插入节点即可,好好模拟想一想,画一画,应该能想通。
34+
35+
``` java
36+
/**
37+
* Definition for a binary tree node.
38+
* public class TreeNode {
39+
* int val;
40+
* TreeNode left;
41+
* TreeNode right;
42+
* TreeNode(int x) { val = x; }
43+
* }
44+
*/
45+
class Solution {
46+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
47+
List<List<Integer>> list = new LinkedList<>();
48+
helper(list, root, 0);
49+
return list;
50+
}
51+
52+
private void helper(List<List<Integer>> list, TreeNode root, int level) {
53+
if (root == null) return;
54+
if (level >= list.size()) {
55+
list.add(0, new LinkedList<>());
56+
}
57+
helper(list, root.left, level + 1);
58+
helper(list, root.right, level + 1);
59+
list.get(list.size() - level - 1).add(root.val);
60+
}
61+
}
62+
```
63+
64+
65+
## 结语
66+
67+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
68+
69+
70+
71+
[title]: https://leetcode.com/problems/binary-tree-level-order-traversal-ii
72+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+97-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

0 commit comments

Comments
 (0)