题目
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL)
return true;
int left_depth = getdepth(pRoot->left);
int right_depth = getdepth(pRoot->right);
if(left_depth>right_depth+1 || left_depth+1<right_depth)
return false;
else
return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}
int getdepth(TreeNode* pRoot){
if(pRoot==NULL)return 0;
return max(1+getdepth(pRoot->left),1+getdepth(pRoot->right));
}
};