思路很简单,树的最大高度和最小高度之差不能超过1.
The idea is very simple, the difference of minimum height and the maximum height should not exceed 1.
struct Node{
Node* left;
Node* right;
};
int MaxHeight(Node* root)
{
if (root == NULL)
{
return 0;
}
return 1+max(MaxHeight(root->left),MaxHeight(root->right));
}
int MinHeight(Node *root)
{
if (root == NULL)
{
return 0;
}
return 1+min(MinHeight(root->left),MinHeight(root->right));
}
bool IsBalancde(Node * root)
{
if (root == NULL)
{
return true;
}
return (MaxHeight(root) - MinHeight(root))<= 1;
}