Skip to content

Commit 503e322

Browse files
committed
Runtime: 3 ms (Top 94.64%) | Memory: 42.3 MB (Top 95.72%)
1 parent e84923f commit 503e322

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
// Runtime: 3 ms (Top 94.64%) | Memory: 42.3 MB (Top 95.72%)
12
/**
23
* Definition for a binary tree node.
34
* public class TreeNode {
4-
* int val;
5-
* TreeNode left;
6-
* TreeNode right;
7-
* TreeNode() {}
8-
* TreeNode(int val) { this.val = val; }
9-
* TreeNode(int val, TreeNode left, TreeNode right) {
10-
* this.val = val;
11-
* this.left = left;
12-
* this.right = right;
13-
* }
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode() {}
9+
* TreeNode(int val) { this.val = val; }
10+
* TreeNode(int val, TreeNode left, TreeNode right) {
11+
* this.val = val;
12+
* this.left = left;
13+
* this.right = right;
14+
* }
1415
* }
1516
*/
1617
class Solution {
17-
18+
1819
private static class MNode {
1920
TreeNode Node;
2021
int hDist;
@@ -25,42 +26,42 @@ private static class MNode {
2526
level = l;
2627
}
2728
}
28-
29+
2930
public List<List<Integer>> verticalTraversal(TreeNode root) {
3031
Map<Integer, PriorityQueue<MNode>> map = new TreeMap<>();
3132
Queue<MNode> q = new LinkedList<>();
32-
33+
3334
q.add(new MNode(root, 0, 0));
34-
35+
3536
while(!q.isEmpty()) {
36-
37+
3738
MNode curr = q.poll();
38-
if(map.containsKey(curr.hDist))
39-
map.get(curr.hDist).add(curr);
40-
39+
if(map.containsKey(curr.hDist))
40+
map.get(curr.hDist).add(curr);
41+
4142
else {
4243
PriorityQueue<MNode> pq = new PriorityQueue<>
4344
((a,b) -> (a.level == b.level)? a.Node.val - b.Node.val: a.level - b.level);
44-
pq.add(curr);
45+
pq.add(curr);
4546
map.put(curr.hDist, pq);
4647
}
47-
48+
4849
if(curr.Node.left != null)
4950
q.add(new MNode(curr.Node.left, curr.hDist -1, curr.level + 1));
50-
51+
5152
if(curr.Node.right != null)
52-
q.add(new MNode(curr.Node.right, curr.hDist +1, curr.level + 1));
53+
q.add(new MNode(curr.Node.right, curr.hDist +1, curr.level + 1));
5354
}
54-
55+
5556
List<List<Integer>> ans = new ArrayList<>();
56-
for(Integer key: map.keySet()) {
57+
for(Integer key: map.keySet()) {
5758
List<Integer> temp = new ArrayList<>();
5859
while(!map.get(key).isEmpty()) { temp.add(map.get(key).poll().Node.val); }
5960
ans.add(new ArrayList<>(temp));
6061
}
61-
62+
6263
return ans;
63-
64+
6465
}
65-
66-
}
66+
67+
}

0 commit comments

Comments
 (0)