题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:
int Depth(TreeNode* root)
{
if(!root)
return 0;
else
return 1+max(Depth(root->left),Depth(root->right));
}
bool IsBalanced_Solution(TreeNode* pRoot)
{
if(!pRoot)
return true;
return (abs(Depth(pRoot->left)-Depth(pRoot->right))<=1)&&IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}
};
本文介绍了一种算法,用于判断给定的二叉树是否为平衡二叉树。通过递归计算每个节点的左右子树深度,并比较深度差是否不超过1来实现。

14万+

被折叠的 条评论
为什么被折叠?



