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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count all k-sum paths in a Binary Tree
- Count even paths in Binary Tree
- Count of Fibonacci paths in a Binary tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print all Exponential Levels of a Binary Tree
- Print all k-sum paths in a binary tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all the paths from root, with a specified sum in Binary tree
- Print Palindromic Paths of Binary tree
- Given a binary tree, print all root-to-leaf paths
- Root to leaf paths having equal lengths in a Binary Tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Count the Number of Binary Search Trees present in a Binary Tree
- Count of 1's in any path in a Binary Tree
- Count of subtrees in a Binary Tree having XOR value K
- Count Non-Leaf nodes in a Binary Tree
- Count of subtrees in a Binary Tree having bitwise OR 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 firstname.lastname@example.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.