class Solution {
public:    
    bool isBalanced(TreeNode *root) {
        if (checkDepth(root) == -1) return false;
        else return true;
    }
    int checkDepth(TreeNode *root) {
        if (!root) return 0;
        int left = checkDepth(root->left);
        if (left == -1) return -1;
        int right = checkDepth(root->right);
        if (right == -1) return -1;
        int diff = abs(left - right);
        if (diff > 1) return -1;
        else return 1 + max(left, right);
    }
};

相关文章:

  • 2021-09-20
  • 2021-07-20
  • 2021-10-07
  • 2021-07-18
  • 2021-08-23
  • 2021-11-28
  • 2021-08-23
  • 2022-12-23
猜你喜欢
  • 2021-07-23
  • 2021-08-22
  • 2021-09-08
相关资源
相似解决方案