Count of exponential paths in a Binary Tree
Given a Binary Tree, the task is to count the number of Exponential paths in the given Binary Tree.
Exponential Path is a path where root to leaf path contains all nodes being equal to xy, & where x is a minimum possible positive constant & y is a variable positive integer.
Input: 27 / \ 9 81 / \ / \ 3 10 70 243 / \ 81 909 Output: 2 Explanation: There are 2 exponential path for the above Binary Tree, for x = 3, Path 1: 27 -> 9 -> 3 Path 2: 27 -> 81 -> 243 -> 81 Input: 8 / \ 4 81 / \ / \ 3 2 70 243 / \ 81 909 Output: 1
- First find the value of x for which xy=root & x is minimum possible & y>0.
- If current value of the node is not equal to xy for some y>0, or pointer becomes NULL then return the count.
- If the current node is a leaf node then increment the count by 1.
- Recursively call for the left and right subtree with the updated count.
- After all recursive call, the value of count is number of exponential paths for a given binary tree.
Below is the implementation of the above approach:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.