-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay-7 Vertical Order Traversal of a Binary Tree
61 lines (55 loc) · 1.52 KB
/
Day-7 Vertical Order Traversal of a Binary Tree
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Location> locations=null;
public List<List<Integer>> verticalTraversal(TreeNode root) {
List<List<Integer>> result=new ArrayList<>();
locations=new ArrayList<>();
dfs(root,0,0);
Collections.sort(locations);
int x=locations.get(0).x;
result.add(new ArrayList<>());
for(Location l: locations){
if(x!=l.x){
x=l.x;
result.add(new ArrayList<>());
}
result.get(result.size()-1).add(l.val);
}
return result;
}
private void dfs(TreeNode root,int x,int y){
if(root!=null){
locations.add(new Location(x,y,root.val));
dfs(root.left,x-1,y-1);
dfs(root.right,x+1,y-1);
}
}
class Location implements Comparable<Location>{
int x,y,val;
Location(int x,int y,int val){
this.x=x;
this.y=y;
this.val=val;
}
@Override
public int compareTo(Location l){
if(this.x!=l.x){
return Integer.compare(this.x,l.x);
}
if(this.y!=l.y){
return Integer.compare(l.y, this.y);
}
else {
return Integer.compare(this.val,l.val);
}
}
}
}