-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbinaryTreeVerticalOrderTraversal.java
More file actions
129 lines (104 loc) · 3.46 KB
/
binaryTreeVerticalOrderTraversal.java
File metadata and controls
129 lines (104 loc) · 3.46 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// // Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).
// If two nodes are in the same row and column, the order should be from left to right.
// Examples 1:
// Input: [3,9,20,null,null,15,7]
// 3
// /\
// / \
// 9 20
// /\
// / \
// 15 7
// Output:
// [
// [9],
// [3,15],
// [20],
// [7]
// ]
//TC: O(n) since peforming BFS over all nodes in the tree
//SC: O(n) queue could grow max size of number of nodes in tree
class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
Queue<Integer> cols = new LinkedList<>();
HashMap<Integer, List<Integer>> map = new HashMap<>(); // (col , list of nodes in col)
int min=0;
int max=0;
q.add(root);
cols.add(0);
while(!q.isEmpty())
{
TreeNode node = q.poll();
int col = cols.poll();
if(!map.containsKey(col))
{
map.put(col, new ArrayList<Integer>());
}
map.get(col).add(node.val);
if(node.left!=null)
{
q.add(node.left);
cols.add(col-1);
min=Math.min(min, col-1);
}
if(node.right!=null)
{
q.add(node.right);
cols.add(col+1);
max=Math.max(max, col+1);
}
}
for(int i=min; i<=max; i++)
{
res.add(map.get(i));
}
return res;
}
}
IF solution requires them to be sorted if in same level
//use temporary map to group ndoes by row, so we can sort before inserting into the map
class Solution {
public List<List<Integer>> verticalTraversal(TreeNode root) {
List<List<Integer>> res = new ArrayList();
if(root==null) return res;
Queue<TreeNode> qt = new LinkedList();
Queue<Integer> qi = new LinkedList();
int min=0, max=0;
Map<Integer, List<Integer>> map = new HashMap();
qt.add(root);
qi.add(0);//not root.val
while(!qt.isEmpty()){
int size = qt.size();
Map<Integer,List<Integer>> tmp = new HashMap(); //really just a row mapping
for(int i=0;i<size;i++){
TreeNode cur = qt.poll();
int idx = qi.poll();
if(!tmp.containsKey(idx)) tmp.put(idx, new ArrayList<Integer>());
tmp.get(idx).add(cur.val);
if(idx<min) min = idx;
if(idx>max) max = idx;
if(cur.left!=null){
qt.add(cur.left);
qi.add(idx-1);
}
if(cur.right!=null){
qt.add(cur.right);
qi.add(idx+1);
}
}
for(int key : tmp.keySet()){
if(!map.containsKey(key)) map.put(key, new ArrayList<Integer>());
List<Integer> list = tmp.get(key); //sort by level
Collections.sort(list);
map.get(key).addAll(list);
}
}
for (int i=min; i<=max; i++){
res.add(map.get(i));
}
return res;
}
}