forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerticalOrderTraversal-2
More file actions
56 lines (52 loc) · 1.45 KB
/
VerticalOrderTraversal-2
File metadata and controls
56 lines (52 loc) · 1.45 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
class Q
{
int hd;
Node temp;
Q(Node t, int h)
{
temp = t;
hd = h;
}
}
class BinaryTree
{
static int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
static void findVerticalOrder(Node root, HashMap<Integer, ArrayList<Integer>> hm)
{
if(root == null)
return;
Queue<Q> q = new LinkedList<Q>();
q.add(new Q(root, 0));
while(!q.isEmpty())
{
Q obj = q.pop();
ArrayList<Integer> al = hm.get(obj.hd);
if(al == null)
al = new ArrayList<Integer>();
al.add(obj.temp.data);
hm.put(obj.hd, al);
if(obj.temp.left != null)
q.add(new Q(obj.temp.left, obj.hd-1));
if(obj.temp.right != null)
q.add(new Q(obj.temp.right, obj.hd+1));
if(obj.hd < min)
min = obj.hd;
if(obj.hd > max)
max = obj.hd;
}
}
static void verticalOrder(Node root)
{
HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
int hd = 0;
findVerticalOrder(root, hm);
for(int i=min ; i<=max ; i++)
{
ArrayList<Integer> al = hm.get(i);
if(al == null)
continue;
for(int j=0 ; j<al.size() ; j++)
System.out.print(al.get(j) + " ");
}
}
}