由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 用queue 做树的广度优先遍历,空间复杂度是多少?
相关主题
Level order traversal只让用一个Queue怎么做?如何实现binary tree的从下到上的分层打印?
求救: 打印binary tree这个用stack实现queue
请问一个简单的面试题G电面题
A家一道onsite题FLG面经:如何分块pre-order遍历一棵树?
问个老题find kth smallest key in BST with O(lgn)
这个题目有什么trick哪里有简单基础题的题库啊?... 跪在简单题上了..
print bst in level order dfs为什么是O(N)不应该是O(N^2)吗?我恨iPhone@Facebook电面
bloomberg onsite题amazon二面
相关话题的讨论汇总
话题: got话题: queue话题: 做树话题: 复杂度话题: nodes
进入JobHunting版参与讨论
1 (共1页)
b****z
发帖数: 176
1
用queue 做树的广度优先遍历,空间复杂度是多少?
感觉好像不是O(n), 应该会比O(n)小? 因为 空间复杂度应该是 树的底部的节点的数
量,因为每次都会dequeue。
大家觉得呢?
x****g
发帖数: 1512
2
性质不变,最大(N+1)/2 ?

【在 b****z 的大作中提到】
: 用queue 做树的广度优先遍历,空间复杂度是多少?
: 感觉好像不是O(n), 应该会比O(n)小? 因为 空间复杂度应该是 树的底部的节点的数
: 量,因为每次都会dequeue。
: 大家觉得呢?

w*****h
发帖数: 423
3
树的底部节点数最多就n/2啊

【在 b****z 的大作中提到】
: 用queue 做树的广度优先遍历,空间复杂度是多少?
: 感觉好像不是O(n), 应该会比O(n)小? 因为 空间复杂度应该是 树的底部的节点的数
: 量,因为每次都会dequeue。
: 大家觉得呢?

b*****a
发帖数: 70
4
Assuming you are talking about tree traversal of a binary tree. If n is the
total number of nodes, the space complexity of BFS using queue is O(n / 2).
A*********c
发帖数: 430
5
The basic definition of complexity is "worst case", so it is just O(n).
Imagine a tree with 2 levels.
First level is root.
Second level contains 1000 nodes.
Even if the queue does not store all the nodes, it is still the Order of N.
so it is just plain O(n).
light?

【在 b****z 的大作中提到】
: 用queue 做树的广度优先遍历,空间复杂度是多少?
: 感觉好像不是O(n), 应该会比O(n)小? 因为 空间复杂度应该是 树的底部的节点的数
: 量,因为每次都会dequeue。
: 大家觉得呢?

b****z
发帖数: 176
6

Thank you ! I got it !!

【在 A*********c 的大作中提到】
: The basic definition of complexity is "worst case", so it is just O(n).
: Imagine a tree with 2 levels.
: First level is root.
: Second level contains 1000 nodes.
: Even if the queue does not store all the nodes, it is still the Order of N.
: so it is just plain O(n).
: light?

b****z
发帖数: 176
7

Thank you ! I got it !!

【在 x****g 的大作中提到】
: 性质不变,最大(N+1)/2 ?
b****z
发帖数: 176
8

Thank you ! I got it !!

【在 w*****h 的大作中提到】
: 树的底部节点数最多就n/2啊
b****z
发帖数: 176
9

the
Thank you ! I got it !!

【在 b*****a 的大作中提到】
: Assuming you are talking about tree traversal of a binary tree. If n is the
: total number of nodes, the space complexity of BFS using queue is O(n / 2).

1 (共1页)
进入JobHunting版参与讨论
相关主题
amazon二面问个老题
问一道二叉树遍历的问题? 谢谢!这个题目有什么trick
请教一道Leetcode 题,多谢print bst in level order dfs为什么是O(N)不应该是O(N^2)吗?
面试题bloomberg onsite题
Level order traversal只让用一个Queue怎么做?如何实现binary tree的从下到上的分层打印?
求救: 打印binary tree这个用stack实现queue
请问一个简单的面试题G电面题
A家一道onsite题FLG面经:如何分块pre-order遍历一棵树?
相关话题的讨论汇总
话题: got话题: queue话题: 做树话题: 复杂度话题: nodes