class Solution {
public:
int Deepth(TreeNode* pRoot){
if(pRoot == NULL)
return 0;
int left = Deepth(pRoot -> left);
int right = Deepth(pRoot -> right);
if(abs(left - right) > 1){
is_balance = false;
return 0;
}
return max(left, right) + 1;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == NULL)
return true;
Deepth(pRoot);
return is_balance;
}
private:
bool is_balance = true;
};

本文介绍了一种用于检测二叉树是否为平衡二叉树的算法实现。通过递归计算二叉树的深度,并检查左右子树深度之差是否超过1来判断。该算法在遍历每个节点时,同时进行深度计算和平衡性验证。
340

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



