Root to leaf path product equal to a given number
Given a binary tree and a number, return true if the tree has a root-to-leaf path such that product of all the values along that path equals the given number. Return 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 leaf node of the current path of the tree.
Below is the implementation of the above approach:
C++
// C++ program to check if there exists // a root to leaf path product with // given product #include <bits/stdc++.h> using namespace std; // Binary Tree Node struct node { int data; struct node* left; struct node* right; }; // Function to check if there exists a path // with given product // Strategy: divide the node value from the product // if it is divisible when recurring down, and check // to see if the product is 1 when you reach leaf // node of the current path out of tree. bool hasPathProduct(struct node* node, int prod) { // return true if we run out // of tree and prod==1 if (node == NULL) { return (prod == 1); } else { bool ans = 1; // Check if product is divisible by // current node, if not we are on wrong path if (prod % (node->data)) return false; // otherwise check both subtrees int subProduct = prod / node->data; // If we reach a leaf node and prod // becomes 1 then return true 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; } } /* UTILITY FUNCTIONS */// Helper function that allocates // a new node with the given data // and NULL left and right pointers struct node* newnode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return (node); } // Driver Code int main() { int prod = 400; /* Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 */ 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
// Java program to check if there exists // a root to leaf path product with // given product class GFG { // Binary Tree Node static class node { int data; node left; node right; }; // Function to check if there exists a path // with given product // Strategy: divide the node value from the product // if it is divisible when recurring down, and check // to see if the product is 1 when you reach leaf // node of the current path out of tree. static boolean hasPathProduct(node node, int prod) { // return true if we run out // of tree and prod==1 if (node == null) { return (prod == 1); } else { boolean ans = true; // Check if product is divisible by // current node, if not we are on wrong path if (prod % (node.data) == 1) { return false; } // otherwise check both subtrees int subProduct = prod / node.data; // If we reach a leaf node and prod // becomes 1 then return true 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; } } /* UTILITY FUNCTIONS */ // Helper function that allocates // a new node with the given data // and null left and right pointers static node newnode(int data) { node node = new node(); node.data = data; node.left = null; node.right = null; return (node); } // Driver Code public static void main(String[] args) { int prod = 400; /* Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 */ 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"); } } } // This code is contributed by Princi Singh |
Python3
# Python3 program to check if there exists # a root to leaf path product with # given product """ UTILITY FUNCTIONS """# Helper function that allocates # a new node with the given data # and None left and right pointers class newnode: def __init__(self, data): self.data = data self.left = self.right = None # Function to check if there exists # a path with given product # Strategy: divide the node value from # the product if it is divisible when # recurring down, and check to see if # the product is 1 when you reach leaf # node of the current path out of tree. def hasPathProduct(node, prod) : # return true if we run out # of tree and prod==1 if (node == None) : return (prod == 1) else : ans = 1 # Check if product is divisible by # current node, if not we are on wrong path if (prod % (node.data)) : return False # otherwise check both subtrees subProduct = prod // node.data # If we reach a leaf node and prod # becomes 1 then return true 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 # Driver Code if __name__ == '__main__': prod = 400 """ Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 """ 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") # This code is contributed # by SHUBHAMSINGH10 |
C#
// C# program to check if there exists // a root to leaf path product with // given product using System; class GFG { // Binary Tree Node public class node { public int data; public node left; public node right; }; // Function to check if there exists a path // with given product // Strategy: divide the node value from the product // if it is divisible when recurring down, and check // to see if the product is 1 when you reach leaf // node of the current path out of tree. static bool hasPathProduct(node node, int prod) { // return true if we run out // of tree and prod==1 if (node == null) { return (prod == 1); } else { bool ans = true; // Check if product is divisible by // current node, if not we are on wrong path if (prod % (node.data) == 1) { return false; } // otherwise check both subtrees int subProduct = prod / node.data; // If we reach a leaf node and prod // becomes 1 then return true 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; } } /* UTILITY FUNCTIONS */ // Helper function that allocates // a new node with the given data // and null left and right pointers static node newnode(int data) { node node = new node(); node.data = data; node.left = null; node.right = null; return (node); } // Driver Code public static void Main(String[] args) { int prod = 400; /* Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 */ 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"); } } } // This code is contributed by Princi Singh |
Output:
YES
Time Complexity : O(n)
Recommended Posts:
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
- Find if there is a pair in root to a leaf path with sum equals to root's data
- GCD from root to leaf path in an N-ary tree
- Check if there is a root to leaf path with given sequence
- Root to leaf path with maximum distinct nodes
- Sum of nodes on the longest path from root to leaf node
- Print the first shortest root to leaf path in a Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Root to leaf paths having equal lengths in a Binary Tree
- Print the longest leaf to leaf path in a Binary tree
- Print root to leaf paths without using recursion
- Sum of all the numbers that are formed from root to leaf paths
- Print all root to leaf paths with there relative positions
- Remove nodes on root to leaf paths of length < K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



