0

so i am new to this programming and i had a doubt on this part of learning it , for two variation problems nearly the code is similar with a little bit of change but i dont understand it clearly.

The Node class type is a standard Tree based structure along with a next self-referential

Connect level order siblings:

class Solution {
public:
    Node* connect(Node* root) {
        
        return helper(root);
        
    }
    
    Node* helper(Node* root)
    {
        if(root==NULL)           
            return root;
        
        queue<Node*>queue;
        queue.push(root);
        
        while(!(queue.empty()))
        {
            int levelsize=queue.size();
            Node* prev=NULL;
            
            for(int i = 0  ; i < levelsize ; i++)
            {
                Node* current=queue.front();
                queue.pop();
                
                if(prev!=NULL)
                    prev->next=current;
                
                prev=current;
                
                if(current->left!=NULL)
                    queue.push(current->left);
                if(current->right!=NULL)
                    queue.push(current->right);
                
            }   
        }

        return root;
        
    }
    
};

Connect all level order siblings:

class Solution {
public:
    Node* connect(Node* root) 
    {
       return helper(root);
    }
    
    Node* helper(Node* root)
    {
        if(root==NULL)           
            return root;
        
        queue<Node*>queue;
        queue.push(root);
        
        while(!(queue.empty()))
        {
            int levelsize=queue.size();
            Node* prev=NULL;
           
                Node* current=queue.front();
                queue.pop();
                
                if(prev!=NULL)
                    prev->next=current;
                
                prev=current;
                
                if(current->left!=NULL)
                    queue.push(current->left);
                if(current->right!=NULL)
                    queue.push(current->right);
                
          
          
        }

        return root;
        
    }
    
};
eis
  • 51,991
  • 13
  • 150
  • 199
John Mellow
  • 116
  • 5
  • 1
    Hi. what is the question here? – eis May 13 '21 at 03:16
  • im not getting how these two algorithms differ, when they are same for(int i = 0 ; i < levelsize ; i++) this piece of code isnt in the second algorithm , i wanted to know how both of these differs – John Mellow May 13 '21 at 04:32
  • please add description of what you want to know to the question itself – eis May 13 '21 at 04:53
  • Well it looks like the second one doesn't have a for loop nested inside the while loop. Thus when iterating through the nodes of the first one will connect all nodes on the same level. While the second one will connect all nodes into a giant linkedlist. – dawooshifingerhold May 13 '21 at 20:39
  • ahh now i understood thanks alot – John Mellow May 18 '21 at 14:14

0 Answers0