{
If (head==NULL)
return 0;
Int a=height (head - & gt; Lchild);
Int b=height (head - & gt; Rchild);
The return of Max (a, b) + 1;
}
Bool balancetree (node * head)
{
if (! The head)
return true;
If (abs (height (head - & gt; Lchild) - height (head - & gt; Rchild) & lt;
=1)If (balancetree (head - & gt; Lchild))
If (balancetree (head - & gt; Rchild))
return true;
return false;
}
CodePudding user response:
https://blog.csdn.net/qq_21201267/article/details/100783394You can refer to the blog
bool isBalanced (root TreeNode *) {
Bool bal=true;
Balence (root, bal);
Return bal.
}
Int balence (root TreeNode * and bool & amp; Bal)
{
if(! Bal)
return -1;
If (root==NULL)
return 0;
Int left_height=balence (root - & gt; Left, bal);
Int right_height=balence (root - & gt; Right, bal);
If (abs (left_height - right_height) & gt; 1)
Bal=false;
The return of Max (left_height right_height) + 1;
}