Given a Binary Tree, the task is to print all the Exponential Levels in the given Binary Tree.
An Exponential Level is a level whose all nodes of that levels equals to xy, & where x is a minimum possible positive constant & y is a variable positive integer.
Input: 20 / \ 9 81 / \ / \ 3 10 70 243 / \ 81 909 Output: 20 9 81 Explanation: There are 2 exponential levels: 20: 201 = 20. 9, 81: 32 = 9, 34 = 81. Input: 8 / \ 4 81 / \ / \ 5 125 625 5 / \ 81 909 Output: 8 5 125 625 5
Approach: To solve the problem mentioned above the main idea is to use Level Order Tree Traversal.
- Perform level order traversal of the given Binary tree and store each level in a vector.
- Then, in each level, if every node can be expressed in the form of xy, for y ≥ 0.
- If any value of the node of this level is not equal to xy, then skip to the next level.
- Print all such levels in which the above condition is true.
Below is the implementation of the above approach:
20 9 81 3 9 81 243
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. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.