My recursive cpp code


  • 2
    C
    class Solution {
    public:
        TreeNode* helper(TreeNode* root)
        {
            TreeNode* left = root->left, *right = root->right;
    
            TreeNode* tail = root;
            if (left)
            {
                tail = helper(left);
                root->right = left;
                root->left = NULL;
            }
            if (right)
            {
                TreeNode* rtail = helper(right);
                tail->right = right;
                tail = rtail;
            }
    
            return tail;
        }
    
        void flatten(TreeNode* root)
        {
            if (!root) return;
    
            TreeNode* tail = helper(root);
            tail->right = NULL;
        }
    };

Log in to reply
 

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