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