6ms c++ solution!


  • 0
    S

    class Solution {
    public:
    void flatten(TreeNode* root) {
    traversal(root);
    }
    TreeNode* traversal(TreeNode* root){
    if(!root)
    return NULL;
    TreeNode* left = traversal(root->left);
    TreeNode* right = traversal(root->right);
    if(left){
    TreeNode* tmp = root->right;
    root->right = root->left;
    left->right = tmp;
    root->left = NULL;
    }
    if(right)
    return right;
    else if(left)
    return left;
    else
    return root;
    }
    };


Log in to reply
 

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