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:
Time Complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
- Check if there is a root to leaf path with given sequence
- Print the longest leaf to leaf path in a Binary tree
- Find the maximum sum leaf to root path in a Binary Tree
- Sum of nodes on the longest path from root to leaf node
- Root to leaf path with maximum distinct nodes
- Print the first shortest root to leaf path in a Binary Tree
- GCD from root to leaf path in an N-ary tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Maximize count of set bits in a root to leaf path in a binary tree
- Find all root to leaf path sum of a Binary Tree
- Root to leaf paths having equal lengths in a Binary Tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Maximum XOR with given value in the path from root to given node in the tree
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value 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 email@example.com. 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.