题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL)
return true;
int leftTree = TreeDepth(pRoot->left);
int rightTree = TreeDepth(pRoot->right);
if(abs(leftTree-rightTree)>1)
return false;
return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right); //所有对应的子树都是平衡树,才是二叉平衡树
}
int TreeDepth(TreeNode* pRoot)
{
if(pRoot == NULL)
return 0;
int left,right;
if(pRoot->left==NULL)
left = 0x80000000;
if(pRoot->right==NULL)
right = 0x80000000;
left = TreeDepth(pRoot->left);
right = TreeDepth(pRoot->right);
return left > right ? left + 1 : right + 1;
}
};