0ms simple recursive solution with explaination


  • 1
    B

    All you need is to swap the left and right pointer of root, and repeat this operation to each child. : )

    TreeNode* invertTree(TreeNode* root) {

        if(root){
            TreeNode* temp = root->left;
            root->left=root->right;
            root->right = temp;
            invertTree(root->left);
            invertTree(root->right);
        }
        return root;
    }

Log in to reply
 

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