My simple C++ 40ms ues a queue


  • -1
    L
    class Solution {
    public:
        void connect(TreeLinkNode *root) 
        {
           if(!root)return ;
           deque<TreeLinkNode*> cur;
           cur.push_back(root);
           while(!cur.empty())
           {
              deque<TreeLinkNode*> next;
              while(!cur.empty())
              {
                TreeLinkNode *out=cur[0];
                cur.pop_front();
                if(out->left)
                {
                  next.push_back(out->left);
                }
                if(out->right)
                {
                  next.push_back(out->right);
                }
              
                if(!cur.empty())
                 out->next=cur[0];
                else
                 out->next=nullptr;
              }
              cur=std::move(next);
             
           }
           
        }
    };

  • 0
    X

    This next stack is not constant space.


Log in to reply
 

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