class Solution {
public:
    bool isBalanced(TreeNode* root) {
        int depth = 0;
        return Balanced(root,depth);
    }
    bool Balanced(TreeNode* root,int& depth){
        if(root == NULL){
            depth = 0;
            return true;
        }
        int left,right;
        if(Balanced(root->left,left) && Balanced(root->right,right)){
            int gap = left - right;
            if(gap <= 1 && gap >= -1){
                depth = left > right ? left + 1 : right + 1;
                return true;
            }
        }
        return false;
    }
};

 

相关文章:

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