-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertical-order-traversal-of-a-binary-tree.java
86 lines (69 loc) · 1.74 KB
/
vertical-order-traversal-of-a-binary-tree.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Comparator;
class Solution {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Point {
TreeNode node;
int x;
int y;
Point(int x, int y, TreeNode node) {
this.x = x;
this.y = y;
this.node = node;
}
}
public List<List<Integer>> verticalTraversal(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Comparator<Point> comp = new Comparator<Point>() {
public int compare(Point p1, Point p2) {
if (p1.x != p2.x) { // compare by x
return p1.x - p2.x;
}
if (p1.y != p2.y) { // compare by y
return p2.y - p1.y;
}
return p1.node.val - p2.node.val; // compare by val
};
};
PriorityQueue<Point> queue = new PriorityQueue<>(10, comp);
traverse(root, 0, 0, queue);
Point prev = null;
List<Integer> l = new ArrayList<>();
while (!queue.isEmpty()) {
Point p = queue.poll();
if (prev == null || p.x != prev.x) {
if (prev != null)
res.add(l);
l = new ArrayList<>();
}
l.add(p.node.val);
prev = p;
}
res.add(l);
return res;
}
private void traverse(TreeNode root, int x, int y, PriorityQueue<Point> queue) {
if (root == null) {
return;
}
queue.offer(new Point(x, y, root));
traverse(root.left, x - 1, y - 1, queue);
traverse(root.right, x + 1, y - 1, queue);
}
}