经典题,需要记忆,且注意fabs和fmax函数的使用
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int deep(struct TreeNode*root)
{
if(root==NULL)
{
return 0;
}
return fmax( deep(root->left),deep(root->right))+1;
}
bool isBalanced(struct TreeNode* root) {
if(root==NULL)
{
return true;
}
if(fabs(deep(root->left)-deep(root->right))>1)
{
return false;
}
if(isBalanced(root->left)&&isBalanced(root->right))
{
return true;
}
return false;
}