题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
平衡二叉树左右子树的高度绝对值之差不会大于1
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if (!pRoot)
return 0;
else
return max(1 + TreeDepth(pRoot->left), 1 + TreeDepth(pRoot->right));
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(!pRoot)return true;
int l = TreeDepth(pRoot->left);
int r = TreeDepth(pRoot->right);
if(l > r + 1 || r > l + 1) return false;
else if (IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right))
return true;
else return false;
}
};