Skip to content

Commit 6dcf4b7

Browse files
107. Binary Tree Level Order Traversal II (java)
1 parent ec4aab4 commit 6dcf4b7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
List<List<Integer>> lists;
3+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
4+
lists = new ArrayList<>();
5+
levelOrderBottom(root,0);
6+
List<List<Integer>> result = new ArrayList<>();
7+
for (int i = lists.size() - 1; i >= 0; i--) result.add(lists.get(i));
8+
return result;
9+
}
10+
private void levelOrderBottom(TreeNode root, int i) {
11+
if (root==null) return;
12+
List<Integer> list;
13+
if (i==lists.size()){
14+
list = new ArrayList<>();
15+
lists.add(list);
16+
}else {
17+
list = lists.get(i);
18+
}
19+
list.add(root.val);
20+
levelOrderBottom(root.left,i+1);
21+
levelOrderBottom(root.right,i+1);
22+
}
23+
}

0 commit comments

Comments
 (0)