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