Given a Binary Tree, the task is to find the maximum value of GCD from any path from the root node to the leaf node.
Examples:
Input: Below is the given tree:

Output: 3
Explanation:
Path 1: 15->3->5 = gcd(15, 3, 15) =3
Path 2: 15->3->1 =gcd(15, 3, 1) = 1
Path 3: 15->7->31=gcd(15, 7, 31)= 1
Path 4: 15->7->9 = gcd(15, 7, 9) =1, out of these 3 is the maximum.
Input: Below is the given tree:

Output: 1
Approach: The idea is to traverse all the paths from the root node to the leaf node and calculate the GCD of all the nodes that occurred in that path. Below are the steps:
- Perform a preorder traversal on the given Binary Tree.
- Iterate over all the paths and track all path values in an array.
- Whenever encountered a leaf value then find the GCD of all the values in an array.
- Update the GCD to the maximum value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxm = 0;
struct Node {
int val;
Node *left, *right;
Node( int x)
{
val = x;
left = NULL;
right = NULL;
}
};
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int find_gcd(vector< int > arr)
{
if (arr.size() == 1)
return arr[0];
int g = arr[0];
for ( int i = 1; i < arr.size(); i++) {
g = gcd(g, arr[i]);
}
return g;
}
void maxm_gcd(Node* root, vector< int > ans)
{
if (!root)
return ;
if (root->left == NULL
and root->right == NULL) {
ans.push_back(root->val);
maxm = max(find_gcd(ans),
maxm);
return ;
}
ans.push_back(root->val);
maxm_gcd(root->left, ans);
maxm_gcd(root->right, ans);
}
int main()
{
Node* root = new Node(15);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(15);
root->left->right = new Node(1);
root->right->left = new Node(31);
root->right->right = new Node(9);
maxm_gcd(root, {});
cout << maxm << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxm = 0 ;
static class Node
{
int val;
Node left, right;
Node( int x)
{
val = x;
left = null ;
right = null ;
}
};
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int find_gcd(Vector<Integer> arr)
{
if (arr.size() == 1 )
return arr.get( 0 );
int g = arr.get( 0 );
for ( int i = 1 ; i < arr.size(); i++)
{
g = gcd(g, arr.get( 1 ));
}
return g;
}
static void maxm_gcd(Node root,
Vector<Integer> ans)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
ans.add(root.val);
maxm = Math.max(find_gcd(ans),
maxm);
return ;
}
ans.add(root.val);
maxm_gcd(root.left, ans);
maxm_gcd(root.right, ans);
}
public static void main(String[] args)
{
Node root = new Node( 15 );
root.left = new Node( 3 );
root.right = new Node( 7 );
root.left.left = new Node( 15 );
root.left.right = new Node( 1 );
root.right.left = new Node( 31 );
root.right.right = new Node( 9 );
maxm_gcd(root, new Vector<>());
System.out.print(maxm + "\n" );
}
}
|
Python3
global maxm
maxm = 0
class Node:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def find_gcd(arr):
if ( len (arr) = = 1 ):
return arr[ 0 ]
g = arr[ 0 ]
for i in range ( 1 , len (arr)):
g = gcd(g, arr[i])
return g
def maxm_gcd(root, ans):
global maxm
if ( not root):
return
if (root.left = = None and
root.right = = None ):
ans.append(root.val)
maxm = max (find_gcd(ans), maxm)
return
ans.append(root.val)
maxm_gcd(root.left, ans)
maxm_gcd(root.right, ans)
if __name__ = = '__main__' :
root = Node( 15 )
root.left = Node( 3 )
root.right = Node( 7 )
root.left.left = Node( 15 )
root.left.right = Node( 1 )
root.right.left = Node( 31 )
root.right.right = Node( 9 )
maxm_gcd(root, [])
print (maxm)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxm = 0;
class Node
{
public int val;
public Node left, right;
public Node( int x)
{
val = x;
left = null ;
right = null ;
}
};
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int find_gcd(List< int > arr)
{
if (arr.Count == 1)
return arr[0];
int g = arr[0];
for ( int i = 1; i < arr.Count; i++)
{
g = gcd(g, arr[1]);
}
return g;
}
static void maxm_gcd(Node root,
List< int > ans)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
ans.Add(root.val);
maxm = Math.Max(find_gcd(ans),
maxm);
return ;
}
ans.Add(root.val);
maxm_gcd(root.left, ans);
maxm_gcd(root.right, ans);
}
public static void Main(String[] args)
{
Node root = new Node(15);
root.left = new Node(3);
root.right = new Node(7);
root.left.left = new Node(15);
root.left.right = new Node(1);
root.right.left = new Node(31);
root.right.right = new Node(9);
maxm_gcd(root, new List< int >());
Console.Write(maxm + "\n" );
}
}
|
Javascript
<script>
let maxm = 0;
class Node
{
constructor(x)
{
this .val = x;
this .left = null ;
this .right = null ;
}
}
var root;
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function find_gcd(arr)
{
if (arr.length == 1)
return arr[0];
let g = arr[0];
for (let i = 1; i < arr.length; i++)
{
g = gcd(g, arr[1]);
}
return g;
}
function maxm_gcd(root, ans)
{
if (root == null )
return ;
if (root.left == null &&
root.right == null )
{
ans.push(root.val);
maxm = Math.max(find_gcd(ans),
maxm);
return ;
}
ans.push(root.val);
maxm_gcd(root.left, ans);
maxm_gcd(root.right, ans);
}
root = new Node(15);
root.left = new Node(3);
root.right = new Node(7);
root.left.left = new Node(15);
root.left.right = new Node(1);
root.right.left = new Node(31);
root.right.right = new Node(9);
let arr = [];
maxm_gcd(root, arr);
document.write(maxm);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Aug, 2021
Like Article
Save Article