C simple recursion


  • 0
    V
    struct TreeNode * invertTree(struct TreeNode * root) 
    {
        if (root == NULL)
            return root;
        
        // swap pointers
        struct TreeNode * tmp = root->left;
        root->left = root->right;
        root->right = tmp;
        
        // repeat for left and right
        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.