Accepted code even if Not full tree beats 85% C++ simple code, using Queue.


  • 0
    S
     void connect(TreeLinkNode *root) {
            if(root==NULL)return;
            queue<TreeLinkNode * > q;
            q.push(root);
            q.push(NULL);
            while(!q.empty()){
                TreeLinkNode* temp = q.front();
                q.pop();
                if(temp==NULL){
                    if(!q.empty()){
                        q.push(NULL);
                    }
                }else{
                   temp->next =  q.front();
                    if(temp->left)q.push(temp->left);
                    if(temp->right)q.push(temp->right);
                }
                    
            }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.