[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5] should not the answer for this tree be false?


  • 0
    N

    /**

    • Definition for a binary tree node.

    • struct TreeNode {

    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      */
      class Solution {
      public:

      int maxDepth(TreeNode* root)
      {
      if(root==NULL)
      return 0;
      return 1+max(maxDepth(root->left),maxDepth(root->right));
      }

      int minDepth(TreeNode* root)
      {
      if(root==NULL)
      return 0;
      return 1+min(minDepth(root->left),minDepth(root->right));
      }

      bool isBalanced(TreeNode* root) {
      return (maxDepth(root)-minDepth(root)<=1);
      }
      };


  • 1
    C

    the answer is true.

    the depth of the right sub tree of the root is 3, and the depth of the left sub tree of the root is 4.

    the definition of "balanced" here is for "the depths of the two sub trees", not "the depths of all the leaves".


  • 0
    N

    thank you for answering my doubt!:)


  • 0
    J

    you are right ,at first i wrote the same code like him,thank you.


  • 0
    W

    You are right. I previously was checking is_balance_tree in my mind.
    Thanks.


  • 0
    A

    This problem is misleading. There is no such a thing as "depth of a tree" (or subtree). It should be changed to "the height of each subtree".


  • 0
    2
    This post is deleted!

Log in to reply
 

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