判断一颗二叉树是否是平衡二叉树
/
解题思路:
首先要保证当前树的左右子树高度差不大于1,并且子树本身也是平衡树。*
/
int maxDepth(struct TreeNode root){
return root ? 1 + fmax(maxDepth(root->left) , maxDepth(root->right)) : 0;
}
bool isBalanced(struct TreeNode* root){
if(root == NULL)
return true;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return abs(left - right) < 2
&& isBalanced(root->left)
&& isBalanced(root->right);
}