C++ O(1) space solution


  • 0
    J
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            if(root==NULL)
            {
                return;
            }
            
            TreeLinkNode * ln = root;
            while(ln->left && ln->right)
            {
                TreeLinkNode * temp = ln;
                while(temp)
                {
                    temp->left->next = temp->right;
                        
                    if(temp->next)
                    {
                        temp->right->next = temp->next->left;
                    }
                    
                    temp = temp->next;
                }
                
                ln = ln->left;
            }
            
            return;
        }
    };

Log in to reply
 

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