题目描述
判断给定的二叉树是否是平衡的
在这个问题中,定义平衡二叉树为每个节点的左右两个子树高度差的绝对值不超过1的二叉树
class Solution {
public:
int height(TreeNode* root)
{
if(root == NULL)
return 0;
return max(height(root->left), height(root->right))+1;
}
bool isBalanced(TreeNode* root)
{
if(root == NULL)
return true;
if(height(root->left)-height(root->right)<=1 && height(root->left)-height(root->right)>=-1
&& isBalanced(root->left) && isBalanced(root->right))
return true;
return false;
}
};