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