Print all Exponential Levels of a Binary Tree


In this problem, we will print all exponential levels of the binary tree.

We will use the level order traversal to traverse through each level of the binary tree. After that, we will find minimum P and q values using the first element of the level. In the next step, we can check whether other level values are exponential.

Problem Statement

We have given a binary tree. We need to print values of all exponential levels of the binary tree. It is given if the values of each node of the level of the binary tree are equal to PQ. It is called the exponential level. Here P is the minimum constant, and Q is the power of P.

Sample Examples

Input

	2
 /   \
3     9

Output

2 
3, 9

Explanation

Here, both levels of the tree are exponential. The first level contains only single values. So, it will always be exponential. The second-level values are 31 and 32. So, each values are in the form of 3Q.

Input

         2
       /   \
     16    64
    /  \  /  \
   5  15 20  10
        /      \
       10       100

Output

25 
16 64 
10 100

Explanation

The first, second, and fourth levels are exponential.

Approach

In this approach, we will get all nodes of each level. After that, we will convert the first node into the PQ format. After that, we will check whether we can convert each node to PX format. If yes, we will print the level.

Algorithm

  • Step 1 − Call the executeSieve() to find the prime numbers in the range of N + 1 using the sieve algorithm.

  • Step 1.2 − In the executeSieve() function, define the check[] array of size N + 1 and initialize with ture, assuming all numbers are initially prime.

  • Step 1.3 − Use the loop to make iterations in the range of 2 to p*p. In the loop, if check[p] is true, insert the p into the prime list. Also, updated the values of multiples of p to false in the check[] array.

  • Step 2 − Call the showExpoLevels() function. In the showExpoLevels() function, call the getHeight() function to get the height of the binary tree. In the getHeight() function, traverse the tree recursively and get the maximum height of the tree.

  • Step 3 − After that, create a queue[] array of size equal to the height of the tree and initialize the first element with the root node. After that, execute the getExpLevels() function.

  • Step 3.1 − In the getExpLevels() function, create a level[] list to store each level node.

  • Step 3.2 − Make iterations while the index is less than the size. Also, use the nested loop to make iterations while the index is less than the temp size to traverse a particular level.

  • Step 3.3 − Take the first node from the 'ind' index of the queue. Insert the node values into the levels[] list. If the left and right node exists, insert them into the 'queue' list at the end.

  • Step 3.4 − Increment the 'ind' value.

  • Step 3.5 − If we have traversed a particular level, call the checkForExponential() function to check whether the current level is exponential.

  • Step 3.5.1 − In the checkForExponential() function, call the getXValue() function to get the value of the x.

  • Step 3.5.2 − In the getXValue() function, if n is 1, return 1.

  • Step 3.5.3 − Otherwise, take the log of n, and make a traversal from 2 to n to find the base for log(n).

  • Step 3.5.4 − Take a log of q. Divide the log(n)/log(q). After that, take (pow(q, int(power)), and if the resultant value is equal to n, return x from the function.

  • Step 3.6 − After getting the X value, check whether each level value is the power of X by using the isVal() function.

  • Step 3.6.1 − In the isVal() function, we use the logarithms to check whether we can format the given value in the power of X.

  • Step 3.7 − If we can't format any value of level in the power of X, return false. Otherwise, return true at last.

  • Step 4 − If level is exponential level, print all values of level.

Example

#include <bits/stdc++.h>
using namespace std;
int N = 1e6;

struct TreeNode {
   int val;
   struct TreeNode *left, *right;
};
TreeNode *newNode(int val) {
   TreeNode *temp = new TreeNode;
   temp->val = val;
   temp->left = temp->right = NULL;
   return (temp);
}
vector<int> prime;
void executeSieve() {
   // Initially, all numbers are prime
   bool check[N + 1];
   memset(check, true, sizeof(check));
   for (int p = 2; p * p <= N; p++) {
      if (check[p] == true) {
         prime.push_back(p);
         // Update multiple p
         for (int q = p * p; q <= N; q += p)
            check[q] = false;
      }
   }
}
// To check whether number x^n
bool is_val(int n, int x) {
   double n_log;
   // Get a log
   n_log = log10(n) / log10(x);
   int num = (int)(pow(x, int(n_log)));
   if (n == num)
      return true;
   return false;
}
int getXValue(int n) {
   if (n == 1)
      return 1;
   double logVal, logq, power;
   int x, number;
   logVal = log10(n);
   for (int q = 2; q <= n; q++) {
      logq = log10(q);
      power = logVal / logq;
      number = (int)(pow(q, int(power)));
      if (abs(number - n) < 1e-6) {
         x = q;
         break;
      }
   }
   return x;
}
bool checkForExponential(vector<int> &level) {
   // Get the value of X
   int x = getXValue(level[0]);
   for (int q = 1; q < level.size(); q++) {
      if (!is_val(level[q], x))
         return false;
   }
   return true;
}
void getExpLevels(struct TreeNode *root, struct TreeNode *queue[], int ind, int size) {
   vector<int> level;
   while (ind < size) {
      int tmp_size = size;
      while (ind < tmp_size) {
         struct TreeNode *temp = queue[ind];
         level.push_back(temp->val);
         if (temp->left != NULL)
            queue[size++] = temp->left;
         if (temp->right != NULL)
            queue[size++] = temp->right;
         // Increment ind
         ind++;
      }
      // check if the level is exponential
      if (checkForExponential(level)) {
         for (auto ele : level) {
            cout << ele << " ";
         }
         cout << endl;
      }
      level.clear();
   }
}
int getHeight(struct TreeNode *root) {
   // Base case
   if (root == NULL)
      return 0;
   return 1 + getHeight(root->left) + getHeight(root->right);
}
void showExpoLevels(struct TreeNode *node) {
   int treeSize = getHeight(node);
   // Create a queue
   struct TreeNode *queue[treeSize];
   // Push root node in a queue
   queue[0] = node;
   getExpLevels(node, queue, 0, 1);
}
int main() {
   TreeNode *root = newNode(25);
   root->left = newNode(16);
   root->right = newNode(64);
   root->left->left = newNode(5);
   root->left->right = newNode(15);
   root->right->left = newNode(20);
   root->right->right = newNode(10);
   root->right->left->left = newNode(10);
   root->right->right->right = newNode(100);
   executeSieve();
   showExpoLevels(root);
   return 0;
}

Output

25 
16 64 
10 100
  • Time complexity − O(N*N*logN)

  • Space complexity − O(N*N) for calculating prime numbers.

Conclusion

Here, we printed each exponential level of binary tree. However, problem is not beginner friendly, but beginner programmers can try to write code to print all even or odd levels of binary tree. If any level of binary tree contains only even number, level is called even level, and same goes for the odd levels.

Updated on: 25-Aug-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements