Root to leaf path product equal to a given number
Last Updated :
09 Sep, 2022
Given a binary tree and a number, the return is true if the tree has a root-to-leaf path such that the product of all the values along that path equals the given number. The return is false if no such path can be found.
For example, in the above tree, there exist three roots to leaf paths with the following products.
- 240 –> 10 – 8 – 3
- 400 –> 10 – 8 – 5
- 40 –> 10 – 2 – 2
Approach: The idea is to start traversing the tree recursively and divide the current node’s value from the product if it is divisible when recurring down, and check to see if the product is 1 when you reach the leaf node of the current path of the tree.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
bool hasPathProduct( struct node* node, int prod)
{
if (node == NULL) {
return (prod == 1);
}
else {
bool ans = 1;
if (prod % (node->data))
return false ;
int subProduct = prod / node->data;
if (subProduct == 1 && node->left == NULL
&& node->right == NULL)
return 1;
if (node->left)
ans = hasPathProduct(node->left, subProduct);
if (node->right)
ans = hasPathProduct(node->right, subProduct);
return ans;
}
}
struct node* newnode( int data)
{
struct node* newNode = new node();
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return (newNode);
}
int main()
{
int prod = 400;
struct node* root = newnode(10);
root->left = newnode(8);
root->right = newnode(2);
root->left->left = newnode(3);
root->left->right = newnode(5);
root->right->left = newnode(2);
if (hasPathProduct(root, prod))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG
{
static class node
{
int data;
node left;
node right;
};
static boolean hasPathProduct(node node, int prod)
{
if (node == null )
{
return (prod == 1 );
}
else
{
boolean ans = true ;
if (prod % (node.data) == 1 )
{
return false ;
}
int subProduct = prod / node.data;
if (subProduct == 1 && node.left == null
&& node.right == null )
{
return true ;
}
if (node.left != null )
{
ans = hasPathProduct(node.left, subProduct);
}
if (node.right != null )
{
ans = hasPathProduct(node.right, subProduct);
}
return ans;
}
}
static node newnode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void main(String[] args)
{
int prod = 400 ;
node root = newnode( 10 );
root.left = newnode( 8 );
root.right = newnode( 2 );
root.left.left = newnode( 3 );
root.left.right = newnode( 5 );
root.right.left = newnode( 2 );
if (hasPathProduct(root, prod))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
class newnode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def hasPathProduct(node, prod) :
if (node = = None ) :
return (prod = = 1 )
else :
ans = 1
if (prod % (node.data)) :
return False
subProduct = prod / / node.data
if (subProduct = = 1 and
node.left = = None and
node.right = = None ) :
return 1
if (node.left) :
ans = hasPathProduct(node.left,
subProduct)
if (node.right) :
ans = hasPathProduct(node.right,
subProduct)
return ans
if __name__ = = '__main__' :
prod = 400
root = newnode( 10 )
root.left = newnode( 8 )
root.right = newnode( 2 )
root.left.left = newnode( 3 )
root.left.right = newnode( 5 )
root.right.left = newnode( 2 )
if (hasPathProduct(root, prod)) :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public class node
{
public int data;
public node left;
public node right;
};
static bool hasPathProduct(node node, int prod)
{
if (node == null )
{
return (prod == 1);
}
else
{
bool ans = true ;
if (prod % (node.data) == 1)
{
return false ;
}
int subProduct = prod / node.data;
if (subProduct == 1 && node.left == null
&& node.right == null )
{
return true ;
}
if (node.left != null )
{
ans = hasPathProduct(node.left, subProduct);
}
if (node.right != null )
{
ans = hasPathProduct(node.right, subProduct);
}
return ans;
}
}
static node newnode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void Main(String[] args)
{
int prod = 400;
node root = newnode(10);
root.left = newnode(8);
root.right = newnode(2);
root.left.left = newnode(3);
root.left.right = newnode(5);
root.right.left = newnode(2);
if (hasPathProduct(root, prod))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function hasPathProduct( node , prod)
{
if (node == null )
{
return (prod == 1);
}
else
{
var ans = true ;
if (prod % (node.data) == 1)
{
return false ;
}
var subProduct = prod / node.data;
if (subProduct == 1 && node.left == null
&& node.right == null )
{
return true ;
}
if (node.left != null )
{
ans = hasPathProduct(node.left, subProduct);
}
if (node.right != null )
{
ans = hasPathProduct(node.right, subProduct);
}
return ans;
}
}
function newnode(data)
{
var node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
var prod = 400;
var root = newnode(10);
root.left = newnode(8);
root.right = newnode(2);
root.left.left = newnode(3);
root.left.right = newnode(5);
root.right.left = newnode(2);
if (hasPathProduct(root, prod))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...