forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerticalOrderTraversal-1
38 lines (34 loc) · 1.06 KB
/
VerticalOrderTraversal-1
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
class BinaryTree
{
static int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
static void findVerticalOrder(Node root, HashMap<Integer, ArrayList<Integer>> hm, int hd)
{
if(root == null)
return;
ArrayList<Integer> al = hm.get(hd);
if(al == null)
al = new ArrayList<Integer>();
al.add(root.data);
hm.put(hd, al);
if(hd < min)
min = hd;
if(hd > max)
max = hd;
findVerticalOrder(root.left, hm, hd-1);
findVerticalOrder(root.right, hm, hd+1);
}
static void verticalOrder(Node root)
{
HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
int hd = 0;
findVerticalOrder(root, hm, 0);
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) + " ");
}
}
}