剑指offer-刷题笔记-简单题-JZ79 判断是不是平衡二叉树
class Solution {
public:
int GetTreeDepth(TreeNode* pRoot)
{
int depth = 0;
if(pRoot == nullptr)
{
depth = 0;
}else{
depth = max(GetTreeDepth(pRoot->left),GetTreeDepth(pRoot->right))+1;
}
return depth;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == nullptr || pRoot->left == nullptr && pRoot->right == nullptr)
{
return true;
}
else if (abs(GetTreeDepth(pRoot->left)- GetTreeDepth(pRoot->right)) > 1)
{
return false;
}
return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}
};