The Wayback Machine - https://web.archive.org/web/20210219092756/https://www.geeksforgeeks.org/check-if-nodes-in-top-view-of-a-binary-tree-forms-a-palindrome-number-or-not/
Skip to content
Related Articles

Related Articles

Check if Nodes in Top view of a Binary Tree forms a Palindrome Number or not
  • Last Updated : 19 Feb, 2021

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Structure of the node
// of a Binary Tree
struct Node {
    Node* left;
    Node* right;
    int hd;
    int data;
};
  
// Function to create a new node
Node* newNode(int key)
{
    Node* node = new Node();
    node->left = node->right = NULL;
    node->data = key;
    return node;
}
  
// Function to check if array
// is a palindrome or not
void isPalindrome(vector<int> arr)
{
    // Store the size of arr[]
    int n = arr.size();
  
    // Initialise flag to 0
    int flag = 0;
  
    // Iterate till half the array length
    for (int i = 0; i <= n / 2
                    && n != 0;
         i++) {
  
        // If the first and last
        // array elements are different
        if (arr[i] != arr[n - i - 1]) {
            flag = 1;
            break;
        }
    }
  
    // If flag is set, then print No
    if (flag == 1)
        cout << "No";
    else
        cout << "Yes";
}
  
// Function to find the top view
// of the given Binary Tree
void topview(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
  
    // Stores the nodes while
    // performing tree traversal
    queue<Node*> q;
  
    map<int, int> m;
  
    int hd = 0;
    root->hd = hd;
  
    // Push node and horizontal
    // distance into the queue
    q.push(root);
  
    while (q.size()) {
  
        hd = root->hd;
  
        if (m.count(hd) == 0)
            m[hd] = root->data;
  
        // If root's left child exists
        if (root->left) {
            root->left->hd = hd - 1;
            q.push(root->left);
        }
  
        // If root's right child exists
        if (root->right) {
            root->right->hd = hd + 1;
            q.push(root->right);
        }
        q.pop();
        root = q.front();
    }
  
    // Store the top view in a vector
    vector<int> v;
  
    // Traverse the map
    for (auto i = m.begin();
         i != m.end(); i++) {
  
        // Insert the element in v
        v.push_back(i->second);
    }
  
    // Function call to check if
    // vector v is palindromic or not
    isPalindrome(v);
}
  
// Driver Code
int main()
{
    // Binary Tree Formation
    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;
}

chevron_right


Output:

Yes

Time Complexity: O(N)
Auxiliary Space: O(N)

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :