-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL107.java
More file actions
45 lines (44 loc) · 1.67 KB
/
L107.java
File metadata and controls
45 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;
public class L107 {
/**
* 107. Binary Tree Level Order Traversal II https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
*
* @timeComplexity O(n)
* @spaceComplexity O(n)
*/
static class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
Map<Integer, List<Integer>> result = new HashMap<>();
Map<TreeNode, Integer> levels = new HashMap<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
levels.put(root, 0);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
if (current == null) {
continue;
}
int currentLevel = levels.get(current);
if (!result.containsKey(currentLevel)) {
result.put(currentLevel, new ArrayList<>());
}
result.get(currentLevel).add(current.val);
queue.add(current.left);
queue.add(current.right);
levels.put(current.left, currentLevel + 1);
levels.put(current.right, currentLevel + 1);
}
return result.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey, Comparator.reverseOrder())).map(Map.Entry::getValue).collect(Collectors.toList());
}
}
}