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