Skip to content

Commit d441768

Browse files
Update 107-Binary-Tree-Level-Order-Traversal-II.java
1 parent a326bfc commit d441768

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

java/107-Binary-Tree-Level-Order-Traversal-II.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
//Same just reverse in the end.
1+
//Same just reverse in the end.
22
//Reversing in the end is better than using add(0, E) in case of arraylist as it's an O(1) operation.
3+
34
class Solution {
45
public List<List<Integer>> levelOrderBottom(TreeNode root) {
56
List<List<Integer>> ans = new ArrayList<>();
@@ -19,13 +20,7 @@ public List<List<Integer>> levelOrderBottom(TreeNode root) {
1920
}
2021
int i = 0 , j = ans.size()-1;
2122
//reverse the list
22-
while (i<j) {
23-
List<Integer> cur = ans.get(i);
24-
ans.set(i, ans.get(j));
25-
ans.set(j, cur);
26-
i++;
27-
j--;
28-
}
23+
Collections.reverse(ans);
2924
return ans;
3025
}
3126
}

0 commit comments

Comments
 (0)