No-recursion C++ solution


  • 2
    K
    void flatten(TreeNode* root) {
        if (!root)
            return;
        
        TreeNode* p = root;
        while (p)
        {
            if (!p->left)
            {
                p = p->right;                
            }
            else
            {
                TreeNode* tmp = p->left;
                while(tmp->right != nullptr)
                    tmp = tmp->right;
                tmp->right = p->right;
                p->right = p->left;
                p->left = nullptr;
            }
        }
    }

Log in to reply
 

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