树遍历的时间复杂度是多少?
树遍历的时间复杂度是多少,我相信它一定是显而易见的,但我可怜的大脑现在无法计算出来。
What is the time complexity of tree traversal, I'm sure it must be obvious but my poor brain can not work it out right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您执行的遍历类型和算法,但通常为 O(n),其中 n 是树中节点的总数。深度优先遍历的规范递归实现将按最深级别的顺序消耗内存(在堆栈上),在平衡树上为 log(n)。
It depends what kind of traversal you are performing and the algorithm, but typically it would be O(n) where n is the total number of nodes in the tree. The canonical recursive implementation of depth first traversal, will consume memory (on the stack) in the order of the deepest level, which on a balanced tree it would be log(n).
对于具有 n 个节点的树来说,这不是只是 n 吗?
你每片树叶都会参观一次,不是吗?所以我会说它是线性的。
Wouldn't that just be n for a tree with n nodes?
You visit each tree-leave once, don't you? So i'd say it is linear.