jayati

Symmetric Tree

Oct 11th, 2023
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. //{ Driver Code Starts
  2. //Initial Template for C++
  3.  
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define MAX_HEIGHT 100000
  8.  
  9. // Tree Node
  10. struct Node {
  11.     int data;
  12.     Node* left;
  13.     Node* right;
  14. };
  15.  
  16. // Utility function to create a new Tree Node
  17. Node* newNode(int val) {
  18.     Node* temp = new Node;
  19.     temp->data = val;
  20.     temp->left = NULL;
  21.     temp->right = NULL;
  22.  
  23.     return temp;
  24. }
  25.  
  26.  
  27. // Function to Build Tree
  28. Node* buildTree(string str) {
  29.     // Corner Case
  30.     if (str.length() == 0 || str[0] == 'N') return NULL;
  31.  
  32.     // Creating vector of strings from input
  33.     // string after spliting by space
  34.     vector<string> ip;
  35.  
  36.     istringstream iss(str);
  37.     for (string str; iss >> str;) ip.push_back(str);
  38.  
  39.     // Create the root of the tree
  40.     Node* root = newNode(stoi(ip[0]));
  41.  
  42.     // Push the root to the queue
  43.     queue<Node*> queue;
  44.     queue.push(root);
  45.  
  46.     // Starting from the second element
  47.     int i = 1;
  48.     while (!queue.empty() && i < ip.size()) {
  49.  
  50.         // Get and remove the front of the queue
  51.         Node* currNode = queue.front();
  52.         queue.pop();
  53.  
  54.         // Get the current node's value from the string
  55.         string currVal = ip[i];
  56.  
  57.         // If the left child is not null
  58.         if (currVal != "N") {
  59.  
  60.             // Create the left child for the current node
  61.             currNode->left = newNode(stoi(currVal));
  62.  
  63.             // Push it to the queue
  64.             queue.push(currNode->left);
  65.         }
  66.  
  67.         // For the right child
  68.         i++;
  69.         if (i >= ip.size()) break;
  70.         currVal = ip[i];
  71.  
  72.         // If the right child is not null
  73.         if (currVal != "N") {
  74.  
  75.             // Create the right child for the current node
  76.             currNode->right = newNode(stoi(currVal));
  77.  
  78.             // Push it to the queue
  79.             queue.push(currNode->right);
  80.         }
  81.         i++;
  82.     }
  83.  
  84.     return root;
  85. }
  86.  
  87.  
  88. // } Driver Code Ends
  89. /*
  90. Structure of the node of the tree is as
  91. struct Node {
  92.     int data;
  93.     Node *left;
  94.     Node *right;
  95.  
  96.     Node(int val) {
  97.         data = val;
  98.         left = right = NULL;
  99.     }
  100. };
  101. */
  102. class Solution{
  103.     public:
  104.     // return true/false denoting whether the tree is Symmetric or not
  105.     bool solve(Node* a,Node* b)
  106.     {
  107.          // Code here
  108.         if(!a && !b)
  109.         {
  110.             return true;
  111.         }
  112.         if(a && b)
  113.         {
  114.              if(a->data!=b->data)
  115.             {
  116.                 return false;
  117.             }
  118.         return solve(a->left,b->right) && solve(a->right,b->left);
  119.         }
  120.         return false;
  121.        
  122.     }
  123.     bool isSymmetric(struct Node* root)
  124.     {
  125.        if(root==NULL)
  126.        {
  127.            return true;
  128.        }
  129.        return solve(root->left,root->right);
  130.     }
  131. };
  132.  
  133. //{ Driver Code Starts.
  134.  
  135. /* Driver program to test size function*/
  136.  
  137.  
  138.  
  139. int main() {
  140.  
  141.    
  142.     int t;
  143.     scanf("%d ", &t);
  144.     while (t--) {
  145.         string s, ch;
  146.         getline(cin, s);
  147.        
  148.         Node* root = buildTree(s);
  149.  
  150.         vector<int> ans;
  151.         Solution ob;
  152.         if(ob.isSymmetric(root)){
  153.             cout<<"True"<<endl;
  154.         }
  155.         else{
  156.             cout<<"False"<<endl;
  157.         }
  158.     }
  159.     return 0;
  160. }
  161.  
  162. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment