C solution for inverting binary tree. 0ms.


  • 1
    J
    struct TreeNode* invertTree(struct TreeNode* root)
    {
      if (root == NULL) {
        return NULL;
      }
    
      struct TreeNode *tmp = root->left;
      root->left = root->right;
      root->right = tmp;
    
      if (root->left != NULL) invertTree(root->left);
      if (root->right != NULL) invertTree(root->right);
      return root;
    }

  • 4
    C
    if (root->left != NULL)
    

    and

    if (root->right != NULL)
    

    are unnecessary...


Log in to reply
 

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