My solution, too easy to explain.


  • 3
    B
    TreeNode* invertTree(TreeNode* root) {
        TreeNode *temp;
        if(root==NULL||(root->left==NULL && root->right==NULL))
            return root;
        temp= root->left;
        root->left=root->right;
        root->right=temp;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }

  • 0
    G

    There's an unstated requirement: are you supposed to mutate the original, or return a new tree that is inverted? But I think I can infer you could create a new tree....


  • 0
    B

    I can't quite understand...Create a new tree is not hard. We can also use recursion to build one.


Log in to reply
 

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