class Solution {
public:
//求深度
int deep(TreeNode* root)
{
if(root == NULL)
return 0;
//求左右子树的深度
int left = deep(root->left);
int right = deep(root->right);
return (left > right) ? left+1 : right+1;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
// write code here
//平衡二叉树:左右子树的深度不超过1
//也就是每个根节点的左右子树的差都不超过1
//思路:先序遍历,分别求根节点的左右子树的深度,然后判断是否超过1
if(pRoot == NULL)
return true;
//判断根节点是否是平衡二叉树
int left = deep(pRoot->left );
int right = deep(pRoot->right);
if(left - right > 1 || left - right < -1)
return false;
//判断左右子树是不是平衡二叉树
return (IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right));
}
};
标签:right,int,JZ79,pRoot,deep,二叉树,平衡,left
From: https://www.cnblogs.com/H43724334/p/18151626