My 11 lines C/C++ solution(recursive)


  • 0
    K
    struct TreeNode* invertTree(struct TreeNode* root) {
        if(!root) return NULL;
        // swap l,r
        struct TreeNode* temp=root->left;
        root->left=root->right;
        root->right=temp;
        // recursion
        if(root->left)
            invertTree(root->left);
        if(root->right)
            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.