jayati

Connect Nodes at Same Level

Oct 13th, 2023
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Tree Node
  6. struct Node
  7. {
  8.     int data;
  9.     Node* left;
  10.     Node* right;
  11.     Node* nextRight;
  12. };
  13.  
  14. // Utility function to create a new Tree Node
  15. Node* newNode(int val)
  16. {
  17.     Node* temp = new Node;
  18.     temp->data = val;
  19.     temp->left = NULL;
  20.     temp->right = NULL;
  21.     temp->nextRight = NULL;
  22.    
  23.     return temp;
  24. }
  25.  
  26. // Function to Build Tree
  27. Node* buildTree(string str)
  28. {  
  29.     // Corner Case
  30.     if(str.length() == 0 || str[0] == 'N')
  31.             return NULL;
  32.    
  33.     // Creating vector of strings from input
  34.     // string after spliting by space
  35.     vector<string> ip;
  36.    
  37.     istringstream iss(str);
  38.     for(string str; iss >> str; )
  39.         ip.push_back(str);
  40.        
  41.     // Create the root of the tree
  42.     Node* root = newNode(stoi(ip[0]));
  43.        
  44.     // Push the root to the queue
  45.     queue<Node*> queue;
  46.     queue.push(root);
  47.        
  48.     // Starting from the second element
  49.     int i = 1;
  50.     while(!queue.empty() && i < ip.size()) {
  51.            
  52.         // Get and remove the front of the queue
  53.         Node* currNode = queue.front();
  54.         queue.pop();
  55.            
  56.         // Get the current node's value from the string
  57.         string currVal = ip[i];
  58.            
  59.         // If the left child is not null
  60.         if(currVal != "N") {
  61.                
  62.             // Create the left child for the current node
  63.             currNode->left = newNode(stoi(currVal));
  64.                
  65.             // Push it to the queue
  66.             queue.push(currNode->left);
  67.         }
  68.            
  69.         // For the right child
  70.         i++;
  71.         if(i >= ip.size())
  72.             break;
  73.         currVal = ip[i];
  74.            
  75.         // If the right child is not null
  76.         if(currVal != "N") {
  77.                
  78.             // Create the right child for the current node
  79.             currNode->right = newNode(stoi(currVal));
  80.                
  81.             // Push it to the queue
  82.             queue.push(currNode->right);
  83.         }
  84.         i++;
  85.     }
  86.    
  87.     return root;
  88. }
  89.  
  90. void connect(struct Node *p);
  91.  
  92. /* Helper function that allocates a new node with the
  93.    given data and NULL left and right pointers. */
  94.  
  95.  
  96. void printSpecial(Node *root)
  97. {
  98.    if (root == NULL)
  99.      return;
  100.  
  101.    Node* next_root=NULL;
  102.  
  103.    while (root != NULL)
  104.    {
  105.       cout<< root->data<<" ";
  106.  
  107.       if( root->left && (!next_root) )
  108.         next_root = root->left;
  109.       else if( root->right && (!next_root)  )
  110.         next_root = root->right;
  111.  
  112.       root = root->nextRight;
  113.    }
  114.    
  115.    printSpecial(next_root);
  116. }
  117.  
  118. void inorder(Node *root)
  119. {
  120.     if (root == NULL)
  121.        return;
  122.     inorder(root->left);
  123.     cout << root->data << " ";
  124.     inorder(root->right);
  125. }
  126.  
  127.  
  128. // } Driver Code Ends
  129. /* struct Node
  130. {
  131.   int data;
  132.   Node *left,  *right;
  133.   Node *nextRight;  // This has garbage value in input trees
  134. }; */
  135.  
  136.  
  137. class Solution
  138. {
  139.     public:
  140.     //Function to connect nodes at same level.
  141.     void connect(Node *root)
  142.     {
  143.        // Your Code Here
  144.        queue<Node*> q;
  145.        if(root==NULL)
  146.        {
  147.            return;
  148.        }
  149.        q.push(root);
  150.        while(!q.empty())
  151.        {
  152.            int n = q.size();
  153.            Node* prev = NULL;
  154.            
  155.            for(int i=0;i<n;i++)
  156.            {
  157.                Node* curr = q.front();
  158.                q.pop();
  159.                if(prev!=NULL)
  160.                {
  161.                    prev->nextRight = curr;
  162.                }
  163.                prev = curr;
  164.                if(curr->left!=NULL)
  165.                {
  166.                    q.push(curr->left);
  167.                }
  168.                if(curr->right!=NULL)
  169.                {
  170.                    q.push(curr->right);
  171.                }
  172.              
  173.            }
  174.              prev=NULL;
  175.            
  176.        }
  177.     }    
  178.      
  179. };
  180.  
  181.  
  182.  
  183.  
  184. //{ Driver Code Starts.
  185.  
  186.  
  187. /* Driver program to test size function*/
  188. int main()
  189. {
  190.   int t;
  191.   scanf("%d\n", &t);
  192.   while (t--)
  193.   {
  194.      string s;
  195.      getline(cin, s);
  196.      Node* root = buildTree(s);
  197.        
  198.     Solution obj;
  199.      obj.connect(root);
  200.      printSpecial(root);
  201.      cout<<endl;
  202.      inorder(root);
  203.      cout<<endl;
  204.   }
  205.   return 0;
  206. }
  207.  
  208. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment