Given a Binary Tree, the task is to count the number of Exponential paths in the given Binary Tree.

Exponential Pathis a path where root to leaf path contains all nodes being equal to x^{y}, & where x is a minimum possible positive constant & y is a variable positive integer.

**Example:**

Input:27 / \ 9 81 / \ / \ 3 10 70 243 / \ 81 909Output:2Explanation:There are 2 exponential path for the above Binary Tree, for x = 3, Path 1: 27 -> 9 -> 3 Path 2: 27 -> 81 -> 243 -> 81Input:8 / \ 4 81 / \ / \ 3 2 70 243 / \ 81 909Output:1

**Approach:** The idea is to use Preorder Tree Traversal. During preorder traversal of the given binary tree do the following:

- First find the value of x for which x
^{y}=root & x is minimum possible & y>0 . - If current value of the node is not equal to x
^{y}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:**

`// C++ program to find the count ` `// exponential paths in Binary Tree ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A Tree node ` `struct` `Node { ` ` ` `int` `key; ` ` ` `struct` `Node *left, *right; ` `}; ` ` ` `// Function to create a new node ` `Node* newNode(` `int` `key) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->key = key; ` ` ` `temp->left ` ` ` `= temp->right ` ` ` `= NULL; ` ` ` `return` `(temp); ` `} ` ` ` `// function to find x ` `int` `find_x(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` ` ` `double` `num, den, p; ` ` ` ` ` `// Take log10 of n ` ` ` `num = ` `log10` `(n); ` ` ` ` ` `int` `x, no; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `den = ` `log10` `(i); ` ` ` ` ` `// Log(n) with base i ` ` ` `p = num / den; ` ` ` ` ` `// Raising i to the power p ` ` ` `no = (` `int` `)(` `pow` `(i, ` `int` `(p))); ` ` ` ` ` `if` `(` `abs` `(no - n) < 1e-6) { ` ` ` `x = i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `x; ` `} ` ` ` `// function To check ` `// whether the given node ` `// equals to x^y for some y>0 ` `bool` `is_key(` `int` `n, ` `int` `x) ` `{ ` ` ` `double` `p; ` ` ` ` ` `// Take logx(n) with base x ` ` ` `p = ` `log10` `(n) / ` `log10` `(x); ` ` ` ` ` `int` `no = (` `int` `)(` `pow` `(x, ` `int` `(p))); ` ` ` ` ` `if` `(n == no) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Utility function to count ` `// the exponent path ` `// in a given Binary tree ` `int` `evenPaths(` `struct` `Node* node, ` ` ` `int` `count, ` `int` `x) ` `{ ` ` ` ` ` `// Base Condition, when node pointer ` ` ` `// becomes null or node value is not ` ` ` `// a number of pow(x, y ) ` ` ` `if` `(node == NULL ` ` ` `|| !is_key(node->key, x)) { ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Increment count when ` ` ` `// encounter leaf node ` ` ` `if` `(!node->left ` ` ` `&& !node->right) { ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Left recursive call ` ` ` `// save the value of count ` ` ` `count = evenPaths( ` ` ` `node->left, count, x); ` ` ` ` ` `// Right reursive call and ` ` ` `// return value of count ` ` ` `return` `evenPaths( ` ` ` `node->right, count, x); ` `} ` ` ` `// function to count exponential paths ` `int` `countExpPaths( ` ` ` `struct` `Node* node, ` `int` `x) ` `{ ` ` ` `return` `evenPaths(node, 0, x); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// create Tree ` ` ` `Node* root = newNode(27); ` ` ` `root->left = newNode(9); ` ` ` `root->right = newNode(81); ` ` ` ` ` `root->left->left = newNode(3); ` ` ` `root->left->right = newNode(10); ` ` ` ` ` `root->right->left = newNode(70); ` ` ` `root->right->right = newNode(243); ` ` ` `root->right->right->left = newNode(81); ` ` ` `root->right->right->right = newNode(909); ` ` ` ` ` `// retrieve the value of x ` ` ` `int` `x = find_x(root->key); ` ` ` ` ` `// Function call ` ` ` `cout << countExpPaths(root, x); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Count even paths in Binary Tree
- Count all k-sum paths in a 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 in a Binary Tree that form an AP
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print all Exponential Levels of a Binary Tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all k-sum paths in a binary tree
- 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 all possible Paths in a Tree such that Node X does not appear before Node Y
- Count of subtrees in a Binary Tree having XOR value K
- Count of 1's in any path in a Binary Tree

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.