import
java.lang.*;
import
java.util.*;
class
Node
{
int
val;
Node left, right;
public
Node(
int
item)
{
val = item;
left = right =
null
;
}
}
class
GFG{
Node root;
static
Map<Integer, Integer> mp =
new
HashMap<>();
static
int
GCD(
int
a,
int
b)
{
if
(b ==
0
)
return
a;
return
GCD(b, a % b);
}
static
void
Trav(Node root,
int
hd)
{
if
(root ==
null
)
return
;
if
(!mp.containsKey(hd))
mp.put(hd, root.val);
else
mp.put(hd, GCD(mp.get(hd), root.val));
Trav(root.left, hd -
1
);
Trav(root.right, hd +
1
);
}
static
void
constructArray(Node root)
{
Trav(root,
0
);
int
lower = Integer.MAX_VALUE, upper =
0
;
for
(Map.Entry<Integer, Integer> it:mp.entrySet())
{
lower = Math.min(lower, it.getKey());
upper = Math.max(upper, it.getKey());
}
ArrayList<Integer> ans =
new
ArrayList<>();
for
(
int
i = lower; i < upper +
1
; i++)
ans.add(mp.getOrDefault(i,
0
));
System.out.print(
"["
);
for
(
int
i =
0
; i < ans.size() -
1
; i++)
System.out.print(ans.get(i) +
", "
);
System.out.print(ans.get(ans.size() -
1
) +
"]"
);
}
public
static
void
main(String[] args)
{
GFG tree =
new
GFG();
Node root =
new
Node(
5
);
root.left =
new
Node(
4
);
root.right =
new
Node(
7
);
root.left.left =
new
Node(
8
);
root.left.left.right =
new
Node(
8
);
root.left.right =
new
Node(
10
);
root.right.right =
new
Node(
6
);
constructArray(root);
}
}