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.
Examples:
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:
// C++ program for printing all
// Exponential levels of binary Tree
#include <bits/stdc++.h>
using namespace std;
int N = 1e6;
// To store all prime numbers
vector<int> prime;
void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..N]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool check[N + 1];
memset(check, true, sizeof(check));
for (int p = 2; p * p <= N; p++) {
// check if prime[p] is not changed,
// then it is a prime
if (check[p] == true) {
prime.push_back(p);
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i <= N; i += p)
check[i] = false;
}
}
}
// 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 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;
}
// 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 Level
// is Exponential or not
bool isLevelExponential(vector<int>& L)
{
// retrieve the value of x
// for that level
int x = find_x(L[0]);
for (int i = 1; i < L.size(); i++) {
// Checking that element is
// equal x^y for some y
if (!is_key(L[i], x))
return false;
}
return true;
}
// Function to print an Exponential level
void printExponentialLevels(vector<int>& Lev)
{
for (auto x : Lev) {
cout << x << " ";
}
cout << endl;
}
// Utility function to get Exponential
// Level of a given Binary tree
void find_ExponentialLevels(struct Node* node,
struct Node* queue[],
int index, int size)
{
vector<int> Lev;
while (index < size) {
int curr_size = size;
while (index < curr_size) {
struct Node* temp = queue[index];
Lev.push_back(temp->key);
// Push left child in a queue
if (temp->left != NULL)
queue[size++] = temp->left;
// Push right child in a queue
if (temp->right != NULL)
queue[size++] = temp->right;
// Increment index
index++;
}
// check if level is exponential
if (isLevelExponential(Lev)) {
printExponentialLevels(Lev);
}
Lev.clear();
}
}
// Function to find total no of nodes
// In a given binary tree
int findSize(struct Node* node)
{
// Base condition
if (node == NULL)
return 0;
return 1
+ findSize(node->left)
+ findSize(node->right);
}
// Function to find Exponential levels
// In a given binary tree
void printExponentialLevels(struct Node* node)
{
int t_size = findSize(node);
// Create queue
struct Node* queue[t_size];
// Push root node in a queue
queue[0] = node;
find_ExponentialLevels(node, queue, 0, 1);
}
// Driver Code
int main()
{
/* 20
/ \
9 81
/ \ / \
3 9 81 243
/ \
81 909 */
// Create Binary Tree as shown
Node* root = newNode(20);
root->left = newNode(9);
root->right = newNode(81);
root->left->left = newNode(3);
root->left->right = newNode(9);
root->right->left = newNode(81);
root->right->right = newNode(243);
root->right->left->left = newNode(81);
root->right->right->right = newNode(909);
// To save all prime numbers
SieveOfEratosthenes();
// Print Exponential Levels
printExponentialLevels(root);
return 0;
}
// Java program for printing all
// Exponential levels of binary Tree
import java.io.*;
import java.util.*;
class GFG {
static int N = (int)1e6;
static List<Integer> prime = new ArrayList<>();
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..N]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
boolean check[] = new boolean[N + 1];
for (int p = 2; p * p <= N; p++)
{
// check if prime[p] is not changed,
// then it is a prime
if (check[p] == true) {
prime.add(p);
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i <= N; i += p) {
check[i] = false;
}
}
}
}
static class Node {
int key;
Node left, right;
}
// Function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return temp;
}
// Function To check
// whether the given node
// equals to x^y for some y>0
static boolean is_key(int n, int x)
{
double p;
// Take logx(n) with base x
p = Math.log10(n) / Math.log10(x);
int no = (int)(Math.pow(x, (int)p));
if (n == no) {
return true;
}
return false;
}
// Function to find x
static int find_x(int n)
{
if (n == 1) {
return 1;
}
double num, den, p;
num = Math.log10(n);
int x = 0;
int no;
for (int i = 2; i <= n; i++) {
den = Math.log10(i);
p = num / den;
no = (int)(Math.pow(i, (int)p));
if (Math.abs(no - n) < 1e-6) {
x = i;
break;
}
}
return x;
}
// Function to check whether Level
// is Exponential or not
static boolean isLevelExponential(List<Integer> L)
{
int x = find_x(L.get(0));
for (int i = 1; i < L.size(); i++)
{
// Checking that element is
// equal x^y for some y
if (!is_key(L.get(i), x)) {
return false;
}
}
return true;
}
static void printExponentialLevels(List<Integer> Lev)
{
for (int i = 0; i < Lev.size(); i++) {
System.out.print(Lev.get(i) + " ");
}
System.out.println();
}
// Utility function to get Exponential
// Level of a given Binary tree
static void find_ExponentialLevels(Node node,
List<Node> queue,
int index, int size)
{
List<Integer> Lev = new ArrayList<Integer>();
while (index < size) {
int curr_size = size;
while (index < curr_size) {
Node temp = queue.get(index);
Lev.add(temp.key);
// Push left child in a queue
if (temp.left != null) {
queue.add(size++, temp.left);
}
// Push right child in a queue
if (temp.right != null) {
queue.add(size++, temp.right);
}
index++;
}
if (isLevelExponential(Lev)) {
printExponentialLevels(Lev);
}
Lev.clear();
}
}
static int findSize(Node node)
{
if (node == null) {
return 0;
}
return 1 + findSize(node.left)
+ findSize(node.right);
}
static void printExponentialLevels(Node node)
{
int t_size = findSize(node);
List<Node> queue = new ArrayList<>(t_size);
queue.add(0, node);
find_ExponentialLevels(node, queue, 0, 1);
}
public static void main(String[] args)
{
Node root = newNode(20);
root.left = newNode(9);
root.right = newNode(81);
root.left.left = newNode(3);
root.left.right = newNode(9);
root.right.left = newNode(81);
root.right.right = newNode(243);
root.right.left.left = newNode(81);
root.right.right.right = newNode(909);
SieveOfEratosthenes();
printExponentialLevels(root);
}
}
// This code is contributed by lokeshmvs21.
# Python3 program for printing
# all Exponential levels of
# binary Tree
import math
# A Tree node
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
# Utility function to create
# a new node
def newNode(key):
temp = Node(key)
return temp
N = 1000000
# Vector to store all the
# prime numbers
prime = []
# Function to store all the
# prime numbers in an array
def SieveOfEratosthenes():
# Create a boolean array "prime[0..N]"
# and initialize all the entries in it
# as true. A value in prime[i]
# will finally be false if
# i is Not a prime, else true.
check = [True for i in range(N + 1)]
p = 2
while(p * p <= N):
# If prime[p] is not
# changed, then it is
# a prime
if (check[p]):
prime.append(p);
# Update all multiples of p
# greater than or equal to
# the square of it
# numbers which are multiples of p
# and are less than p^2
# are already marked.
for i in range(p * p, N + 1, p):
check[i] = False;
p += 1
# Function To check
# whether the given node
# equals to x^y for some y>0
def is_key(n, x):
# Take logx(n) with base x
p = (math.log10(n) /
math.log10(x));
no = int(math.pow(x, int(p)));
if (n == no):
return True;
return False;
# Function to find x
def find_x(n):
if (n == 1):
return 1;
den = 0
p = 0
# Take log10 of n
num = math.log10(n);
x = 0
no = 0;
for i in range(2, n + 1):
den = math.log10(i);
# Log(n) with base i
p = num / den;
# Raising i to the power p
no = int(math.pow(i, int(p)));
if(abs(no - n) < 0.000001):
x = i;
break;
return x;
# Function to check whether Level
# is Exponential or not
def isLevelExponential(L):
# retrieve the value of x
# for that level
x = find_x(L[0]);
for i in range(1, len(L)):
# Checking that element is
# equal x^y for some y
if (not is_key(L[i], x)):
return False;
return True;
# Function to print an
# Exponential level
def printExponentialLevels(Lev):
for x in Lev:
print(x, end = ' ')
print()
# Utility function to get Exponential
# Level of a given Binary tree
def find_ExponentialLevels(node, queue,
index, size):
Lev = []
while (index < size):
curr_size = size;
while index < curr_size:
temp = queue[index];
Lev.append(temp.key);
# Push left child in a queue
if (temp.left != None):
queue[size] = temp.left;
size += 1
# Push right child in a queue
if (temp.right != None):
queue[size] = temp.right;
size += 1
# Increment index
index += 1;
# check if level is exponential
if (isLevelExponential(Lev)):
printExponentialLevels(Lev);
Lev.clear();
# Function to find total no of nodes
# In a given binary tree
def findSize(node):
# Base condition
if (node == None):
return 0;
return (1 + findSize(node.left) +
findSize(node.right));
# Function to find Exponential levels
# In a given binary tree
def printExponentialLevel(node):
t_size = findSize(node);
# Create queue
queue=[0 for i in range(t_size)]
# Push root node in a queue
queue[0] = node;
find_ExponentialLevels(node, queue,
0, 1);
# Driver code
if __name__ == "__main__":
''' 20
/ \
9 81
/ \ / \
3 9 81 243
/ \
81 909 '''
# Create Binary Tree as shown
root = newNode(20);
root.left = newNode(9);
root.right = newNode(81);
root.left.left = newNode(3);
root.left.right = newNode(9);
root.right.left = newNode(81);
root.right.right = newNode(243);
root.right.left.left = newNode(81);
root.right.right.right = newNode(909);
# To save all prime numbers
SieveOfEratosthenes();
# Print Exponential Levels
printExponentialLevel(root);
# This code is contributed by Rutvik_56
// C# program for printing all
// Exponential levels of binary Tree
using System;
using System.Collections.Generic;
class Node {
public int key;
public Node left, right;
}
class GFG {
static int N = (int)1e6;
static List<int> prime = new List<int>();
static void SieveOfEratosthenes()
{
// Create a boolean array "prime[0..N]" and
// initialize all entries it as true. A value in
// prime[i] will finally be false if i is Not a
// prime, else true.
bool[] check = new bool[N + 1];
for (int p = 2; p * p <= N; p++) {
// check if prime[p] is not changed,
// then it is a prime
if (check[p] == true) {
prime.Add(p);
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i <= N; i += p) {
check[i] = false;
}
}
}
}
// Function to create a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return temp;
}
// Function To check
// whether the given node
// equals to x^y for some y>0
static bool is_key(int n, int x)
{
double p;
// Take logx(n) with base x
p = Math.Log(n) / Math.Log(x);
int no = (int)(Math.Pow(x, (int)p));
if (n == no) {
return true;
}
return false;
}
// Function to find x
static int find_x(int n)
{
if (n == 1) {
return 1;
}
double num, den, p;
num = Math.Log10(n);
int x = 0;
int no;
for (int i = 2; i <= n; i++) {
den = Math.Log10(i);
p = num / den;
no = (int)(Math.Pow(i, (int)p));
if (Math.Abs(no - n) < 1e-6) {
x = i;
break;
}
}
return x;
}
// Function to check whether Level
// is Exponential or not
static bool isLevelExponential(List<int> L)
{
int x = find_x(L[0]);
for (int i = 1; i < L.Count; i++) {
// Checking that element is
// equal x^y for some y
if (!is_key(L[i], x)) {
return false;
}
}
return true;
}
static void printExponentialLevels(List<int> Lev)
{
for (int i = 0; i < Lev.Count; i++) {
Console.Write(Lev[i] + " ");
}
Console.Write("\n");
}
// Utility function to get Exponential
// Level of a given Binary tree
static void find_ExponentialLevels(Node node,
List<Node> queue,
int index, int size)
{
List<int> Lev = new List<int>();
while (index < size) {
int curr_size = size;
while (index < curr_size) {
Node temp = queue[index];
Lev.Add(temp.key);
// Push left child in a queue
if (temp.left != null) {
queue.Insert(size++, temp.left);
}
// Push right child in a queue
if (temp.right != null) {
queue.Insert(size++, temp.right);
}
index++;
}
if (isLevelExponential(Lev)) {
printExponentialLevels(Lev);
}
Lev.Clear();
}
}
static int findSize(Node node)
{
if (node == null) {
return 0;
}
return 1 + findSize(node.left)
+ findSize(node.right);
}
static void printExponentialLevels(Node node)
{
int t_size = findSize(node);
List<Node> queue = new List<Node>(t_size);
queue.Insert(0, node);
find_ExponentialLevels(node, queue, 0, 1);
}
public static void Main(string[] args)
{
Node root = newNode(20);
root.left = newNode(9);
root.right = newNode(81);
root.left.left = newNode(3);
root.left.right = newNode(9);
root.right.left = newNode(81);
root.right.right = newNode(243);
root.right.left.left = newNode(81);
root.right.right.right = newNode(909);
SieveOfEratosthenes();
printExponentialLevels(root);
}
}
// This code is contributed by phasing17.
<script>
// JavaScript program for printing all
// Exponential levels of binary Tree
let Lev = []
// A Tree Node
class Node {
constructor(key) {
this.left = null;
this.right = null;
this.key = key;
}
};
// Function to create a new node
function newNode(key)
{
let temp = new Node(key);
return temp;
}
let N = 1e6;
// To store all prime numbers
let prime = [];
function SieveOfEratosthenes()
{
// Create a boolean array "prime[0..N]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
let check = new Array(N + 1);
check.fill(true);
for (let p = 2; p * p <= N; p++) {
// check if prime[p] is not changed,
// then it is a prime
if (check[p] == true) {
prime.push(p);
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (let i = p * p; i <= N; i += p)
check[i] = false;
}
}
}
// Function To check
// whether the given node
// equals to x^y for some y>0
function is_key(n, x)
{
let p;
// Take logx(n) with base x
p = Math.log10(n) / Math.log10(x);
let no = parseInt(Math.pow(x, parseInt(p, 10)), 10);
if (n == no)
return true;
return false;
}
// Function to find x
function find_x(n)
{
if (n == 1)
return 1;
let num, den, p;
// Take log10 of n
num = Math.log10(n);
let x, no;
for (let i = 2; i <= n; i++) {
den = Math.log10(i);
// Log(n) with base i
p = num / den;
// Raising i to the power p
no = parseInt(Math.pow(i, parseInt(p, 10)), 10);
if (Math.abs(no - n) < 1e-6) {
x = i;
break;
}
}
return x;
}
// Function to check whether Level
// is Exponential or not
function isLevelExponential(L)
{
// retrieve the value of x
// for that level
let x = find_x(L[0]);
for (let i = 1; i < L.length; i++) {
// Checking that element is
// equal x^y for some y
if (!is_key(L[i], x))
return false;
}
return true;
}
// Function to print an Exponential level
function printExponentialLevels()
{
for (let x = 0; x < Lev.length; x++) {
document.write(Lev[x] + " ");
}
document.write("</br>");
}
// Utility function to get Exponential
// Level of a given Binary tree
function find_ExponentialLevels(node, queue, index, size)
{
while (index < size) {
let curr_size = size;
while (index < curr_size) {
let temp = queue[index];
Lev.push(temp.key);
// Push left child in a queue
if (temp.left != null)
queue[size++] = temp.left;
// Push right child in a queue
if (temp.right != null)
queue[size++] = temp.right;
// Increment index
index++;
}
// check if level is exponential
if (isLevelExponential(Lev)) {
printExponentialLevels();
}
Lev = [];
}
}
// Function to find total no of nodes
// In a given binary tree
function findSize(node)
{
// Base condition
if (node == null)
return 0;
return 1
+ findSize(node.left)
+ findSize(node.right);
}
// Function to find Exponential levels
// In a given binary tree
function printexponentialLevels(node)
{
let t_size = findSize(node);
// Create queue
let queue = new Array(t_size);
queue.fill(0);
// Push root node in a queue
queue[0] = node;
find_ExponentialLevels(node, queue, 0, 1);
}
/* 20
/ \
9 81
/ \ / \
3 9 81 243
/ \
81 909 */
// Create Binary Tree as shown
let root = newNode(20);
root.left = newNode(9);
root.right = newNode(81);
root.left.left = newNode(3);
root.left.right = newNode(9);
root.right.left = newNode(81);
root.right.right = newNode(243);
root.right.left.left = newNode(81);
root.right.right.right = newNode(909);
// To save all prime numbers
SieveOfEratosthenes();
// Print Exponential Levels
printexponentialLevels(root);
</script>
Output:
20 9 81 3 9 81 243
Time Complexity: O(n2*log(n))
Auxiliary Space: O(n)