An intuitive recursive solution accepted with best submission 8ms in C


  • 0
    struct TreeNode* traverseLeft(struct TreeNode* root)
    {
        if(!root->left &&!root->right) return root;
        struct TreeNode* t = root->right;
        root->right = root->left;
        struct TreeNode *leftmost = root; //in case of NULL left;
        if(root->left)
            leftmost = traverseLeft(root->left);
        root->left = NULL;
        leftmost->right = t;
        if(t)
            traverseLeft(t); 
    }
    void flatten(struct TreeNode* root)
    {
        if(!root) return ;
        traverseLeft(root);
    }

Log in to reply
 

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