https://leetcode.cn/problems/balanced-binary-tree/description/
这题的思路分成子问题就是计算左右子树的高度然后相减看看是不是大于1的就可以了,所以代码如下
int _isBalanced(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
int leftdepth = _isBalanced(root->left);
int rightdepth = _isBalanced(root->right);
return leftdepth > rightdepth ? leftdepth+1 : rightdepth+1;
}
bool isBalanced(struct TreeNode* root) {
if(root == NULL)
{
return true;
}
int ret = abs(_isBalanced(root->left) - _isBalanced(root->right));
if(ret > 1)
{
return false;
}
return isBalanced(root->left) && isBalanced(root->right);
}
————————————水文章了————————————————————