-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17_Jan.java
More file actions
42 lines (38 loc) · 815 Bytes
/
Copy path17_Jan.java
File metadata and controls
42 lines (38 loc) · 815 Bytes
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
class Solution {
int max_gcd = -1;
int ans = -1;
int gcd(int a, int b)
{
if(a==0) return b;
return gcd(b%a, a);
}
int rec(Node root)
{
if(root==null) return -1;
int l = rec(root.left);
int r = rec(root.right);
if(l!=-1 && r!=-1)
{
int gcd = gcd(l, r);
if(max_gcd < gcd)
{
max_gcd = gcd;
ans = root.data;
}
else if(max_gcd == gcd)
{
if(ans<root.data)
{
ans = root.data;
}
}
}
return root.data;
}
int maxGCD(Node root)
{
ans = 0;
rec(root);
return ans;
}
}