A balanced binary tree is defined as follows: the height difference between the left subtree of any node and the right subtree is less than or equal to 1.
Then a binary tree is a balanced binary tree and only when (1, the left subtree is a balanced binary tree, 2. the right subtree is a balanced binary tree; 3. The height difference between the left and right subtree is less than or equal to 1 ).
Therefore, if you use recursive methods to determine whether a recursive function returns two messages: balance or not, tree height. The Code is as follows.
Bool isbalance (node * proot, Int & ndeepth) {If (proot = NULL) {ndeepth = 0; return true;} int deepright; int deepleft; If (! Isbalance (proot-> pright, deepright) |! Isbalance (proot-> pleft, deepleft) return false; ndeepth = 1 + fmax (deepright, deepleft); Return ABS (deepright-deepleft) <= 1 ;}