[c++] Simple and relatively fast


  • 2
    4
    class Solution
    {
    public:
        void connect( TreeLinkNode * root )
        {
            TreeLinkNode * nextLevel = root;
            if( root )
            {
                for( ; root->left; )
                {
                    if( root->left )
                    {
                        root->left->next = root->right;
                    }
                    if( root->next )
                    {
                        root->right->next = root->next->left;
                        root = root->next;
                    }
                    else
                    {
                        root = nextLevel = nextLevel->left;
                    }
                }
            }
        }
    };

Log in to reply
 

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