Skip to content

Commit 0a258bf

Browse files
committed
feat(leetcode): add No.107
1 parent 08192fe commit 0a258bf

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
2+
//
3+
// algorithms
4+
// Easy (47.04%)
5+
// Total Accepted: 229,826
6+
// Total Submissions: 488,538
7+
// beats 100.0% of java submissions
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
20+
LinkedList<List<Integer>> res = new LinkedList<>();
21+
if (root == null) {
22+
return res;
23+
}
24+
25+
LinkedList<TreeNode> q = new LinkedList<>();
26+
ArrayList<Integer> path = new ArrayList<>();
27+
q.add(root);
28+
q.add(null);
29+
30+
while (true) {
31+
TreeNode curItem = q.pollFirst();
32+
if (curItem == null) {
33+
res.offerFirst(new ArrayList<Integer>(path));
34+
if (q.size() == 0) {
35+
break;
36+
}
37+
path.clear();
38+
q.add(null);
39+
} else {
40+
path.add(curItem.val);
41+
if (curItem.left != null) {
42+
q.add(curItem.left);
43+
}
44+
if (curItem.right != null) {
45+
q.add(curItem.right);
46+
}
47+
}
48+
}
49+
50+
return res;
51+
}
52+
}

0 commit comments

Comments
 (0)