Check if any level of a perfect Binary Tree forms a Palindrome
Given a perfect binary tree consisting of N nodes, the task is to check if the number formed by the nodes in any level of the tree forms a palindrome number or not. The root node is not considered to be a palindrome.
Examples:
Input: Tree[][]:
5
/ \
3 3
/ \ / \
6 2 3 6
Output: Yes
Explanation: 3 and 3 makes a number 33 which is a palindrome
Input: Tree[][]:
6
/ \
3 4
/ \ / \
6 2 1 6
Output: False
Explanation: There is no number formed at any level which is palindrome.
Approach: The task can be solved using a breadth-first search over the tree. Follow the below steps to solve the problem:
- Start traversing the tree from the root node
- From the next level onwards, maintain the number formed by concatenating all the nodes at that level
- Check if it is a palindrome or not
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node* left;
Node* right;
int hd;
int data;
};
Node* newNode( int key)
{
Node* node = new Node();
node->left = node->right = NULL;
node->data = key;
return node;
}
bool chkp( int n)
{
string s = to_string(n);
string k = s;
reverse(s.begin(), s.end());
if (k == s)
return true ;
return false ;
}
bool chklevel(Node* root)
{
queue<Node*> q;
q.push(root);
int k = 1;
int p = k;
int n = 0;
while (!q.empty()) {
if (p == 0) {
if (k != 1)
if (chkp(n)) {
return true ;
}
k *= 2;
p = k;
n = 0;
}
Node* t = q.front();
q.pop();
n = n * 10 + t->data;
p--;
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
if (chkp(n))
return true ;
return false ;
}
int main()
{
Node* root = newNode(5);
root->left = newNode(3);
root->right = newNode(3);
root->left->left = newNode(6);
root->left->right = newNode(2);
root->right->right = newNode(6);
root->right->left = newNode(3);
if (chklevel(root))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
static class Node {
Node left;
Node right;
int hd;
int data;
};
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static String reverse(String input) {
char [] a = input.toCharArray();
int l, r = a.length - 1 ;
for (l = 0 ; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
static boolean chkp( int n)
{
String s = String.valueOf(n);
String k = s;
s=reverse(s);
if (k.equals(s))
return true ;
return false ;
}
static boolean chklevel(Node root)
{
Queue<Node> q = new LinkedList<>();
q.add(root);
int k = 1 ;
int p = k;
int n = 0 ;
while (!q.isEmpty()) {
if (p == 0 ) {
if (k != 1 )
if (chkp(n)) {
return true ;
}
k *= 2 ;
p = k;
n = 0 ;
}
Node t = q.peek();
q.remove();
n = n * 10 + t.data;
p--;
if (t.left!= null ) {
q.add(t.left);
}
if (t.right!= null ) {
q.add(t.right);
}
}
if (chkp(n))
return true ;
return false ;
}
public static void main(String[] args)
{
Node root = newNode( 5 );
root.left = newNode( 3 );
root.right = newNode( 3 );
root.left.left = newNode( 6 );
root.left.right = newNode( 2 );
root.right.right = newNode( 6 );
root.right.left = newNode( 3 );
if (chklevel(root))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
class Node:
def __init__( self , key):
self .left = None
self .right = None
self .hd = 0
self .data = key
def chkp(n):
s = str (n)
k = s[:: - 1 ]
if (k = = s):
return True
return False
def chklevel(root):
q = []
q.append(root)
k = 1
p = k
n = 0
while ( len (q) ! = 0 ):
if (p = = 0 ):
if (k ! = 1 ):
if (chkp(n)):
return True
k * = 2
p = k
n = 0
t = q[ 0 ]
q.pop( 0 )
n = n * 10 + t.data
p - = 1
if (t.left ! = None ):
q.append(t.left)
if (t.right ! = None ):
q.append(t.right)
if (chkp(n)):
return True
return False
root = Node( 5 )
root.left = Node( 3 )
root.right = Node( 3 )
root.left.left = Node( 6 )
root.left.right = Node( 2 )
root.right.right = Node( 6 )
root.right.left = Node( 3 )
if (chklevel(root)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
class Node {
public Node left;
public Node right;
public int hd;
public int data;
};
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static String reverse(String input) {
char [] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join( "" ,a);
}
static bool chkp( int n)
{
String s = String.Join( "" ,n);
String k = s;
s=reverse(s);
if (k.Equals(s))
return true ;
return false ;
}
static bool chklevel(Node root)
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
int k = 1;
int p = k;
int n = 0;
while (q.Count!=0) {
if (p == 0) {
if (k != 1)
if (chkp(n)) {
return true ;
}
k *= 2;
p = k;
n = 0;
}
Node t = q.Peek();
q.Dequeue();
n = n * 10 + t.data;
p--;
if (t.left!= null ) {
q.Enqueue(t.left);
}
if (t.right!= null ) {
q.Enqueue(t.right);
}
}
if (chkp(n))
return true ;
return false ;
}
public static void Main(String[] args)
{
Node root = newNode(5);
root.left = newNode(3);
root.right = newNode(3);
root.left.left = newNode(6);
root.left.right = newNode(2);
root.right.right = newNode(6);
root.right.left = newNode(3);
if (chklevel(root))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
class Node {
constructor(key) {
this .left = null ;
this .right = null ;
this .hd = 0;
this .data = key;
}
};
function chkp(n) {
let s = (n).toString();
s = s.split( '' );
let k = [...s];
k = k.join( '' );
s = s.reverse();
s = s.join( '' )
if (k == s)
return true ;
return false ;
}
function chklevel(root) {
let q = [];
q.push(root);
let k = 1;
let p = k;
let n = 0;
while (q.length != 0) {
if (p == 0) {
if (k != 1)
if (chkp(n)) {
return true ;
}
k *= 2;
p = k;
n = 0;
}
let t = q[0];
q.shift();
n = n * 10 + t.data;
p--;
if (t.left != null ) {
q.push(t.left);
}
if (t.right != null ) {
q.push(t.right);
}
}
if (chkp(n))
return true ;
return false ;
}
let root = new Node(5);
root.left = new Node(3);
root.right = new Node(3);
root.left.left = new Node(6);
root.left.right = new Node(2);
root.right.right = new Node(6);
root.right.left = new Node(3);
if (chklevel(root))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...