- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在练习一些面试题,我试图计算出我的解决方案的时间复杂度以确定给定的二叉树是否平衡。
我认为解决方案 2 的时间复杂度为 O(n)
,而解决方案 1 的时间复杂度为 O(n^2)
。这是因为在解决方案 1 中,您向下递归到树的底部以检查树是否平衡并检查子树高度的差异。额外的复杂性来自于我们向树的根部向上移动,get_height 仍然递归到树下以计算高度。因此,再次沿着树向下移动 --> O(n^2)
。
解决方案 2 首先比较高度的事实意味着,当我们向上移动树时,我们不必向下移动来检查子树高度。
helper :
def get_height(root):
if root is None:
return 0
else:
return max(get_height(root.left), get_height(root.right)) + 1
解决方案一:
def is_balanced(root):
if root is None:
return True
else:
return is_balanced(root.right) and is_balanced(root.left) and (abs(get_height(root.left) - get_height(root.right)) < 2)
解决方案 2:
def is_balanced2(root):
if root is None:
return True
else:
return (abs(get_height(root.left) - get_height(root.right)) < 2) and is_balanced(root.right) and is_balanced(root.left)
检查时差的代码:
s = time.time()
print("result: {}".format(is_balanced(a)))
print("time: {}".format(time.time() - s))
s = time.time()
print("result: {}".format(is_balanced2(a)))
print("time: {}".format(time.time() - s))
不平衡树的结果:
result: False
time: 2.90870666504e-05 # solution 1
result: False
time: 1.50203704834e-05 # solution 2
最佳答案
I believe that solution 2 has a time complexity of O(n) while solution 1 has a time complexity of O(n^2).
我不这么认为,如下所述。
...in solution 1, you recurse down to the bottom of the tree to check if the tree is balanced and also check for differences in subtree height. The extra complexity comes as we go higher back up the tree towards the root, the get_height still recurses down the tree to compute the height. Therefore, traveling down the tree once more --> O(n^2).
没那么简单。假设您在根上调用 is_balanced()
:当它递归访问树中的每个节点时,它调用 get_height
递归访问其下的子树。对于根,get_height
几乎访问了整棵树:N-1 次操作,所以 O(N)。对于根的 2 个 child 中的每一个,get_height
访问他们(大致)一半的树:同样是 O(N)。这一直持续到 get_height
对 N/2 叶节点下的 ~N 个 None
节点进行操作:仍然是 O(N)。总的来说,树中有 ~log2N 层,你对每一层进行 O(N) 处理,所以整体复杂度为 O(NlogN)。
The fact that solution 2 compares the height first, means that as we move back up the tree, we dont have to go back down to check the subtree height.
没有。您在解决方案二中更改的是执行 is_balanced
与 get_height
检查的顺序。对于两个测试最终都通过的任何树,处理总量(因此大 O 效率)不变。
另外,您的逻辑不会检查平衡二叉树:您可能需要重新阅读定义。
关于python - 检查二叉树是否平衡的时间复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258139/
我正在尝试了解二叉树的属性。但我不确定一件事: 定义。二叉树的声明: 如果对于每个节点,它认为左子树中的内部节点数和右子树中的内部节点数最多相差 1,则二叉树是平衡的。 如果任意两个叶子的差异都存在,
我有一个带有分片键和索引的集合。但是当我运行平衡时,不会为这个集合移动 block ,因为其他集合 block 正在按预期移动到其他机器。此集合中仅移动了一个 block 。 最佳答案 目前(这将在不
给定一个data.table如下,id1是一个subject-level ID,id2是一个within-subject repeated-measure ID,X 是数据变量,其中有很多。我想平衡数
由于 C++ 集合是在二叉树中实现的,如果我们以递增或递减顺序插入项目,那么集合将更像是一个列表而不是树。有没有什么方法可以在插入项目后平衡树? 最佳答案 C++ 集(即 std::set)通常实现为
我是一名初学者程序员,设计了一个智能手机网站,我有一个主体背景图像,我想慢慢改变颜色平衡,交替颜色,就像有人将 Photoshop 颜色平衡控制条调整一定百分比一样。任一方向。当您查看页面时,这种情况
我开发了一段多线程代码。该代码在 Web 应用程序中调用,因此可能由多个线程(请求)并行调用。为了控制此代码将要创建的线程数量(通过多个并行请求调用),我使用静态共享 ThreadPoolExecut
我正在为 Linux 内核开发一些网络驱动程序。我有几个 if-else 条件,我正在重新分配或释放“skb”结构——这是我有点困惑的地方。关于我在那些 if-else 中做什么 - 我遇到了 2 种
平衡 BST 的最佳和最差搜索性能是什么?每种情况发生时如何用一句话解释? 最佳答案 最佳情况:当搜索到的元素位于树的根部时。你得到 O(1)。 最坏情况:当搜索元素在最长分支的叶子处时,树是单边的。
我在平衡 AVL 树问题上遇到了麻烦,因为我的解决方案似乎与教科书后面的解决方案冲突。我查看了 AVL 树的在线可视化,他们认为我的是正确的。我的课本错了吗? 这是树: 然后我必须将 65 插入到这个
我有一个系统,我在其中使用 RS232 来控制一个灯,该灯接受以浮点形式给出的表示电压(在 2.5 - 7.5 范围内)的输入。然后控件会给出 0 到 6000 范围内的输出,这是传感器拾取的亮度。
我有一个分层目录,每个目录中有很多文件,每个文本文件中有很多 URL 字符串。我想下载 Hadoop 中所有文件中的所有 URL,以实现更好的平衡。 例如,如果我有 1+5 个节点的 Hadoop 集
请查看附件图片,这是一种跷跷板。但从图像来看,黑体具有相同的密度。并且水平矩形使用“Revolute”关节与三角形相连。但仍然没有任何建议。在目前的情况下,它需要平衡。 最佳答案 由于浮点精度等限制导
因此,在平衡 KD 树时,您应该找到中位数,然后将所有较小的元素放在左子树上,将较大的元素放在右子树上。但是,如果您有多个元素与中位数具有相同的值,会发生什么情况?他们进入左子树,右子树还是丢弃它们?
请帮我找到一种干净的方法来从现有数组中创建一个新数组。如果任何类的示例数小于该类中的最大示例数,则应该进行过采样。样本应该从原始数组中提取(随机或顺序都没有区别) 比方说,初始数组是这样的: [ 2
我是一名软件开发人员,但想成为服务器可扩展性领域的新架构师。 在多个服务使用同一数据集的情况下,旨在扩展冗余和负载平衡。 问题是:在一个理想主义的系统中,服务是否应该尝试优化它们的内部处理以减少对远程
假设我有 10 个分区用于 Kafka 中的给定主题。 我的选择是在消费者之间自动平衡这 10 个分区的负载? 我已经阅读了这篇文章 https://stackoverflow.com/a/28580
假设我有一个 B 树,其节点为 3-4 配置(3 个元素和 4 个指针)。假设我按照规则合法地建立我的树,我是否有可能达到一层中有两个节点并且一个节点有 4 个退出指针而另一个节点只有两个退出指针的情
当光标在一个括号上时,如何跳转到配对括号。很高兴在 工作emacs -nw . 就像 % 在 Vim 中。 ;;从@Lindy、@Francesco 得到提示后,我发现了更多: C-M-f
我在平衡 AVL 树时遇到问题。我一直在寻找如何平衡它们的步骤,但我找不到任何有用的东西。 我知道有4种: 单左旋 单右旋 双左右旋转 双左右旋转 但我就是无法得到如何选择其中之一和 在哪个节点上应用
我想获得类似于打印中平衡文本行但用于 block 元素的结果。假设在一个 300/100 像素的容器中有一组 50/50 像素的盒子。在容器中 float 盒子将使它们填满一“行”,然后像这样包裹到下
我是一名优秀的程序员,十分优秀!