Check if Nodes in Top view of a Binary Tree forms a Palindrome Number or not
Last Updated :
04 Apr, 2023
Given a binary tree consisting of N nodes, the task is to check if the nodes in the top view of a Binary Tree forms a palindrome number or not. If found to be a palindrome, then print “Yes”. Otherwise, print “No”.
Examples:
Input:
5
/ \
3 3
/ \ \
6 2 6
Output: Yes
Explanation:
Nodes in the top view are {6, 3, 5, 3, 6}. Hence, the generated number ( = 63536) is a palindrome.
Input:
1
/ \
4 2
Output: No
Approach: To solve the given problem, the idea is to find the top view of the given Binary Tree using the approach discussed in this article and store it in an array, say arr[]. Now, check if the array arr[] is palindrome or not. If found to be true, then print Yes otherwise print No.
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;
}
void isPalindrome(vector< int > arr)
{
int n = arr.size();
int flag = 0;
for ( int i = 0; i <= n / 2
&& n != 0;
i++) {
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break ;
}
}
if (flag == 1)
cout << "No" ;
else
cout << "Yes" ;
}
void topview(Node* root)
{
if (root == NULL)
return ;
queue<Node*> q;
map< int , int > m;
int hd = 0;
root->hd = hd;
q.push(root);
while (q.size()) {
hd = root->hd;
if (m.count(hd) == 0)
m[hd] = root->data;
if (root->left) {
root->left->hd = hd - 1;
q.push(root->left);
}
if (root->right) {
root->right->hd = hd + 1;
q.push(root->right);
}
q.pop();
root = q.front();
}
vector< int > v;
for ( auto i = m.begin();
i != m.end(); i++) {
v.push_back(i->second);
}
isPalindrome(v);
}
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);
topview(root);
return 0;
}
|
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 void isPalindrome(Vector<Integer> arr)
{
int n = arr.size();
int flag = 0 ;
for ( int i = 0 ; i <= n / 2 && n != 0 ; i++)
{
if (arr.get(i) != arr.get(n - i - 1 ))
{
flag = 1 ;
break ;
}
}
if (flag == 1 )
System.out.print( "No" );
else
System.out.print( "Yes" );
}
static void topview(Node root)
{
if (root == null )
return ;
Queue<Node> q = new LinkedList<>();
HashMap<Integer, Integer> m = new HashMap<>();
int hd = 0 ;
root.hd = hd;
q.add(root);
while (q.size() > 0 )
{
hd = root.hd;
if (m.containsKey(hd) && m.get(hd) == 0 )
m.put(hd, root.data);
if (root.left != null )
{
root.left.hd = hd - 1 ;
q.add(root.left);
}
if (root.right != null )
{
root.right.hd = hd + 1 ;
q.add(root.right);
}
q.remove();
root = q.peek();
}
Vector<Integer> v = new Vector<Integer>();
for (Map.Entry<Integer,Integer> i : m.entrySet())
{
v.add(i.getValue());
}
isPalindrome(v);
}
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 );
topview(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
self .hd = 0
def isPalindrome(arr):
n = len (arr)
flag = 0
i = 0
while i < = n / / 2 and n ! = 0 :
if (arr[i] ! = arr[n - i - 1 ]):
flag = 1
break
i + = 1
if (flag = = 1 ):
print ( "No" )
else :
print ( "Yes" )
def topview(root):
if (root = = None ) :
return
q = []
m = dict ()
hd = 0
root.hd = hd
q.append(root)
while ( len (q)) :
root = q[ 0 ]
hd = root.hd
if hd not in m:
m[hd] = root.data
if (root.left) :
root.left.hd = hd - 1
q.append(root.left)
if (root.right):
root.right.hd = hd + 1
q.append(root.right)
q.pop( 0 )
v = []
for i in sorted (m):
v.append(m[i])
isPalindrome(v)
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 )
topview(root)
|
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 void isPalindrome(List< int > arr)
{
int n = arr.Count;
int flag = 0;
for ( int i = 0; i <= n / 2 && n != 0; i++)
{
if (arr[i] != arr[n - i - 1])
{
flag = 1;
break ;
}
}
if (flag == 1)
Console.Write( "No" );
else
Console.Write( "Yes" );
}
static void topview(Node root)
{
if (root == null )
return ;
Queue<Node> q = new Queue<Node>();
Dictionary< int , int > m = new Dictionary< int , int >();
int hd = 0;
root.hd = hd;
q.Enqueue(root);
while (q.Count > 0)
{
hd = root.hd;
if (m.ContainsKey(hd) && m[hd] == 0)
m[hd] = root.data;
if (root.left != null )
{
root.left.hd = hd - 1;
q.Enqueue(root.left);
}
if (root.right != null )
{
root.right.hd = hd + 1;
q.Enqueue(root.right);
}
root = q.Peek();
q.Dequeue();
}
List< int > v = new List< int >();
foreach (KeyValuePair< int , int > i in m)
{
v.Add(i.Value);
}
isPalindrome(v);
}
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);
topview(root);
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .data = key;
}
}
function newNode(key)
{
let node = new Node(key);
return node;
}
function isPalindrome(arr)
{
let n = arr.length;
let flag = 0;
for (let i = 0; i <= parseInt(n / 2, 10) && n != 0; i++)
{
if (arr[i] != arr[n - i - 1])
{
flag = 1;
break ;
}
}
if (flag == 1)
document.write( "No" );
else
document.write( "Yes" );
}
function topview(root)
{
if (root == null )
return ;
let q = [];
let m = new Map();
let hd = 0;
root.hd = hd;
q.push(root);
while (q.length > 0)
{
hd = root.hd;
if (m.has(hd) && m.get(hd) == 0)
m.set(hd, root.data);
if (root.left != null )
{
root.left.hd = hd - 1;
q.push(root.left);
}
if (root.right != null )
{
root.right.hd = hd + 1;
q.push(root.right);
}
q.shift();
root = q[0];
}
let v = [];
m.forEach((values,keys)=>{
v.push(values);
})
isPalindrome(v);
}
let 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);
topview(root);
</script>
|
Time Complexity: O(NlogN), where n is the number of nodes in the binary tree.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...