My c++ solution


  • 0

    class Solution {
    public:
    TreeNode* helper(TreeNode* root){
    if(root == NULL)
    return NULL;
    TreeNode* left_res = helper(root->left);
    TreeNode* right_res = helper(root->right);
    TreeNode* left_end = left_res;
    while(left_end && left_end->right){
    left_end = left_end->right;
    }
    if(left_res){
    root->left = NULL;
    root->right = left_res;
    left_end->right = right_res;
    return root;
    }
    else{
    root->left = NULL;
    root->right = right_res;
    return root;
    }
    return root;
    }
    void flatten(TreeNode* root) {
    helper(root);
    return;
    }
    };


Log in to reply
 

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