/**
* Definition for binary tree
* struct TreeNode {
*? ? int val;
*? ? TreeNode *left;
*? ? TreeNode *right;
*? ? TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
高度平衡二叉樹(shù):
它是一 棵空樹(shù)或它的左右兩個(gè)子樹(shù)的高度差的絕對(duì)值不超過(guò)1征堪,并且左右兩個(gè)子樹(shù)都是一棵平衡二叉樹(shù)
//任何節(jié)點(diǎn)的兩個(gè)兒子子樹(shù)的高度最大差別為一舍咖,被稱為高度平衡樹(shù)
class Solution {
public:
-------------------------------------------------
? int Count(TreeNode *root){? ? ? //計(jì)算樹(shù)的最大深度的函數(shù)
? ? ? if(root==NULL) return 0;
? ? return max(Count(root->left),Count(root->right))+1;
? } ? ?
-----------------------------------------------------
? ? bool isBalanced(TreeNode *root) { ?? //判斷是否是高度平衡二叉樹(shù)
? ? ? ? if(root==NULL)return true;
? ? ? ? if(abs(Count(root->left)-Count(root->right))>1)return false;
? ? ? ? ? ? return isBalanced(root->left)&&isBalanced(root->right);
? ? }
};