# leetcode -day18 Balanced Binary Tree

66 篇文章 0 订阅
1、
﻿﻿

Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

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

02-06 65
09-19 31
04-02 2714
07-11 31
07-14 35
03-20 4058
05-17 3110

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

xiao囡囡

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。