- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我的以下代码中有两个递归层。我想知道是否有可能将两者“合并”,从而使代码更高效?
class Solution(object):
def maxDepth(self, root):
if root == None:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right))+1
def isBalanced(self, root):
if root == None:
return True
if abs(self.maxDepth(root.left) - self.maxDepth(root.right)) <= 1:
return self.isBalanced(root.left) and self.isBalanced(root.right)
return False
最佳答案
要定义一个检查树是否完全平衡的函数,您只能使用以下伪代码中给出的算法访问该树一次(我不太了解 Python 语法来编写显式代码) :
isBalanced(tree):
if tree is null
then return the tuple (true, 0)
else be (bal1, lev1) the result of calling isBalanced on the left child of tree
and be (bal2, lev2) the result of calling isBalanced on the rigth child of tree
if (bal1 is false) or (bal2 is false)
then exit from the function with the tuple (false, 0)
else if lev1 = lev2
then return the tuple (true, 1+lev1)
else exit from the function with the tuple (false, 0)
基本上,该算法会递归地访问树,计算子树是否平衡,以及如果平衡,则计算树的深度。
命令“从函数退出”应该导致立即退出函数的所有递归调用(如果这在 Python 中是可能的),否则它只是从指定元组的当前调用返回。
当然,在函数末尾,只有元组的第一个组成部分(有关平衡性的信息)是有用的。
如果您想检查一棵树是否平衡,叶子的深度差异最多为 1,您可以通过返回具有三个元素(平衡、最小深度、最大深度)的元组来扩展此解决方案,并且在一般情况下检查子级的深度(最小值和最大值)是否一致(然后返回当前的最小值和最大值)。
关于python - python中如何提高判断一棵树是否为AVL树的效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390550/
相信维基百科文章:http://en.wikipedia.org/wiki/AVL_tree AVL trees are height-balanced, but in general not wei
我正在处理 AVL 树分配,我有一个关于它们定义的快速问题 - 我们得到了一个排序列表,我们必须在 O(n) 时间内从中生成一个 AVL 树。我已经完成了这个(感谢 StackOverflow 的其他
AVL 树与自平衡二叉搜索树相同。 AVL 代表什么?和发明者的名字有关系吗? 最佳答案 这是您猜到的发明者的名字。来自 wiki : AVL tree is named after its two
static String min ( AVLStringTreeNode t ) { if( t == null ) return t; while( t.left
一 点睛 平衡二叉查找树,简称平衡二叉树,由苏联数学家 Adelson-Velskii 和 Landis 提出,所以又被称为 AVL 树。 平衡二叉树或为空树,或为具有以下性质的平衡二叉树 1 左右子
更具体地说,是一个 AVL 树。是否可以?我想这样做,但我认为未删除的节点可能难以管理轮换。 我有一个可以正常工作的,但我想将这个带有延迟删除的用于其他用途。 最佳答案 如果您希望它相对于所有节点(包
更具体地说,如果使用 AVL 树而不是哈希表,是否可以更有效地执行任何操作? 最佳答案 我通常更喜欢 AVL 树而不是哈希表。我知道哈希表的预期时间 O(1) 复杂度优于 AVL 树的保证时间 O(l
我正在学习 AVL Tree 并在递归代码中获得了 TLE。我的导师建议迭代解决方案。我搜索并找到了一个将父节点保存在子节点中的解决方案。我想知道这个可能会在内存中出现问题,不是吗?还有另一种方法可以
据我所知AVL之间的时间复杂度树木和 Binary Search Trees在平均情况下是相同的,在最坏的情况下,AVL 会击败 BST。这给了我一个提示,即 AVL 在与它们交互的所有可能方式上总是
我正在用 C 语言实现 AVL 树。我在下面发布了我的树旋转,以及我在尝试测试它们时遇到的 valgrind 错误。 为什么我会收到这些错误?我知道 valgrind 错误源于我使用空指针这一事实,但
我试图理解以下 AVL 树的代码,但遇到了一些困难。我知道如果树很重,它就会向右旋转。同样,如果它是右重,它会向左旋转。如果有人能解释或指出我理解以下代码的正确方向,我将不胜感激。 static vo
我正在为 AVL 树编写右旋转。但它给出了运行时错误。目前我忽略了 AVL 树的高度部分。稍后我会处理这个问题。但是仅在 5 处应用右旋转就会出现问题。请帮助。我使用的AVL树是:
所以我有一个 C 语言的二叉搜索树代码,对我来说效果很好。但是,当我添加 BST 删除代码时,我的程序将在删除过程中崩溃。 它给我一个错误,提示访问冲突读取位置 0x00000000。 我认为这与传递
我正在为 AVL 树编写右旋转。目前我忽略了 AVL 树的高度部分。稍后我会处理这个问题。但是仅在 5 处应用右旋转会给出错误的值。即使执行右旋转后,l->data,ll->data,lll->dat
我在创建 AVL 树时遇到错误,我的代码的输出每次都给出无限循环。 在下面的代码中,mknode函数用于创建一个节点,lr,rl,right,leftfunctions用于执行所需的操作旋转,而插入函
我的 AVL 树是在 Java 中使用一个二维整数数组 avlTree[35][5] 实现的 - 列表示: [0] - 左侧高度 [1] - 左 child [2] - 数据 [3] - 右 chil
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 7 年前。 在第 7 行中,我得到 null
所以,我真的是编程新手,我现在正在上 C++ 类(class),我需要在其中编写和实现 AVL 树,使用双向链表打印树的内容,级别为等级。老师真的很挑剔,所以我们不能使用标准库中的任何容器。我的双向链
我被困在我的任务的一小部分,我基本上需要从 .txt 文件中插入数据(只是随机单词)并将它们作为数据添加到我的 AVL 树中。 我将向您展示我现在拥有的代码,该代码显然无法正常工作,但需要一些有关如何
我正在尝试用 Java 编写一个 AVL 树,并且已经在这个问题上停留了两个晚上。当运行以下代码时,肯定会发生旋转,但最终结果(例如 leftRotate)是我丢失了节点。 public AVLNod
我是一名优秀的程序员,十分优秀!