- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 Cracking the Coding Interview 第 6 版中有一个问题 (4.4),您应该在其中找出二叉树是否平衡,在这种情况下平衡意味着任何一侧是否比另一侧深超过 1。
我这样递归地解决了这个问题:
def isBalanced(root):
return abs(getDepth(root.left) - getDepth(root.right)) > 1
def getDepth(node):
if node is None:
return 0
return 1 + max([getDepth(node.left), getDepth(node.right)])
所以要遍历它。它递归地检查每个节点的每一侧并将其向上传递给根,如果根的左右子树之间的差异大于 1,则返回 False,否则返回 True。
在本书的答案部分,作者写了以下关于此类解决方案的内容:
Although this works, it's not very efficient. On each node, we recurse through it's entire subtree. This means that getHeight is called repeatedly on the same nodes. The algorithm is O(N log N) since each node is "touched" once per node above it.
书籍解决方案如下:
int getHeight(TreeNode root) {
if (root == null) return -1;
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
boolean isBalanced(TreeNode root) {
if (root == null) return true;
int heightDiff = getHeight(root.left) - getHeight(root.right);
if (Math.abs(heightDiff) < 1) {
return false;
} else {
return isBalanced(root.left) && isBalanced(root.right);
}
}
不幸的是,我不明白这是怎么回事。在我看来,该算法只检查它下面的级别。每个节点都未检查多次。当我看这个算法时,它是 O(N)。
谁能帮我确认一下我是否正确理解了这个算法的 Big-O,或者我是否遗漏了什么?
最佳答案
让我们将isBalanced
的时间复杂度函数写成T(n)
。因此,平均案例递归关系是:
其中 O(n)
来自对 getHeight
的两次调用,我们知道这是 O(n)
。因此,使用 Master 定理,isBalanced
的整体复杂度为 O(n log n)
。
您的解决方案没有在子节点上调用 isBalanced
,这意味着上面关系中的 O(n)
被替换为 O(1)
,总体上给出 O(n)
(同样来自 Master 定理)。然而,它并没有(作为一个明显的结果!)检查子节点是否平衡,所以是不正确的。
CTCI 天真的解决方案的问题是它有效地为每个子节点再次调用getHeight
(通过调用isBalanced
),这是不必要的.可以将平衡检查功能合并到 getHeight
中以获得 O(n)
中的解决方案:
int balance_internal(TreeNode root)
{
// change the specification slightly to return 0 for a leaf
// ... and -1 for an unbalanced node
if (root == null) return 0;
int left_h = balance_internal(root.left);
int right_h = balance_internal(root.right);
// if either node is unbalanced
if (left_h == -1 || right_h == -1)
return -1;
// return max height as before
// ... but now with the knowledge that both child nodes are balanced
return Math.abs(left_h - right_h) > 1 ? -1 : 1 + Math.max(left_h, right_h);
}
boolean isBalanced(TreeNode root)
{
return (balance_internal(root) > -1);
}
虽然可能不像提供的解决方案那样优雅,但这不会创建对子节点的重复调用,而是重用第一组调用的结果。因此,递归关系与您自己的解决方案相同,给出 O(n)
。
关于algorithm - 找出二叉树是否平衡的大O(来自CTCI Book),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49235257/
我想在我的 Tree 类中创建一个函数来遍历 n-ary Tree[T] 以取回具有 (level, T) 的元组,以便该 Tree 的用户可以执行类似 tree.traverse.foreach{
给定一个层次格式的数组,它们的直接子级存储在一个连续的数组中,返回一个 n 叉树 给定输入格式: [{'name':'a', 'level': -1}, {'name':'b', 'level
我要求教授给我一份另一个学期的旧作业。它是关于构建一个家谱,然后找到给定的两个节点之间的亲属关系。家谱是关于那美克星人(龙珠z)的,所以每个那美克星人都有一个父亲。 问题是输入是这样的: First
我正在尝试创建一个包含子 vector 的 n 叉树。 这就是我到目前为止所得到的。 在 node.h 文件中我有这个: #include #include using namespa
我正在尝试了解 n 叉树的预序遍历。我一直在阅读,我发现的所有示例都使用左子树和右子树,但是在 n 叉树中,什么是左子树,什么是右子树?有人可以给出一个很好的解释或伪代码吗? 最佳答案 而不是考虑 l
我应该反序列化一个 n 叉树。 这段代码创建了我的树: foodtree.addChildren("Food", { "Plant", "Animal" } ); foodtree.a
我正在尝试创建叉 TreeMap ,但仍然没有成功。这是我的代码: #include #include #include void procStatus(int level) { prin
我有一个二叉树,代表一个解析后的逻辑公式。例如,f = a & b & -c | d 由前缀表示法的列表列表表示,其中第一个元素是运算符(一元或二元),接下来的元素是它们的参数: f = [ |, [
我正在尝试根据给定的输入创建一棵树。那里将有一个根,包括子节点和子子节点。我可以实现树,在其中我可以将子节点添加到特定的主节点(我已经知道根)。但是,我试图弄清楚实现树的推荐方法是什么,我们必须首先从
我在 n 个节点上有一个完整的 19 元树。我标记所有具有以下属性的节点,即它们的所有非根祖先都是最年长或最小的 child (包括根)。我必须为标记节点的数量给出一个渐近界限。 我注意到 第一层有一
如何在不使用递归的情况下遍历 n 叉树? 递归方式: traverse(Node node) { if(node == null) return; for(Node c
我的树/节点类: import java.util.ArrayList; import java.util.List; public class Node { private T data;
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我在我的 Java 应用程序中有一个非 UI 使用的所谓的“k-ary”树,我想知道 javax.swing.tree 包是否是完成这项工作的正确工具,即使它与 Swing 打包在一起. 我有一类 W
我正在用 Java 实现 N 叉树;每个节点可以有尽可能多的节点。当我尝试 build 一棵树时,问题就来了。我有一个函数可以递归地创建一个特定高度的树,并根据节点列表分配子节点。当我调用该函数时,根
嗨,我有这段代码来搜索 n 叉树,但它不能正常工作,我不知道这有什么问题当搜索 n4 和 n5 时,它返回 n3怎么了? public FamilyNode findNodeByName(Family
哪个是 C 语言中 N 叉树的简洁实现? 特别是,我想实现一个 n 元树,而不是自平衡的,每个节点中的子节点数量不受限制,其中每个节点都包含一个已经定义的结构,例如: struct task {
#include #include #include typedef struct _Tree { struct _Tree *child; struct _Tree *
我正在编写文件系统层次结构的 N 叉树表示形式,其中每个节点都包含有关它所表示的文件/文件夹的一些信息。 public class TreeNode { private FileSystemE
如何在 R 中为给定数量的分支和深度构建 N 叉树,例如深度为 3 的二叉树? 编辑:将源问题与问答分开。 最佳答案 我想提出解决方案,我用它来构建树数据结构 叶安姆 分支因子。要将数据存储在树中,字
我是一名优秀的程序员,十分优秀!