时间限制:1秒 空间限制:32768K 热度指数:160212
题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot){
int left=TreeDepth(pRoot->left);
int right=TreeDepth(pRoot->right);
return left>right?left+1:right+1;
}else{
return 0;
}
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot){
int left=TreeDepth(pRoot->left);
int right=TreeDepth(pRoot->right);
if(abs(left-right)>=2){
return false;
}
return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}else{
return true;
}
}
};