Perhaps the most simple code (C++)


  • 4
    Y
    class Solution {
    public:
        TreeNode *sidend = NULL;// this record the left most node;
        void flatten(TreeNode *root) {
            if (root == NULL) return;
            sidend = root;
            TreeNode *r = root->right;
            root->right = root->left;
            root->left = NULL;
            flatten(root->right);
            sidend->right = r;
            flatten(r);
            return ;
        }
    };

Log in to reply
 

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