题目描述:
代码实现:
cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int TreeHeight(struct TreeNode* root)
{
if(root==NULL)
return 0;
//左右子树中大的那个+1
int left=TreeHeight(root->left);
int right=TreeHeight(root->right);
return left>right?left+1:right+1;
}
bool isBalanced(struct TreeNode* root){
if(root==NULL)
return true;
int left=TreeHeight(root->left);
int right=TreeHeight(root->right);
if(left-right>1||right-left>1)
{
return false;
}
return isBalanced(root->left)&&isBalanced(root->right);
}