由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Cormen星号题:O(n)遍历二叉树,只能用O(1) extra space
相关主题
按层遍历二叉树,常量空间,如何做到?面题:copy directed graph
今天面了个老印[合集] 给定一个最小堆,如何查找某数是否存在此堆中?
怎么用lex处理DFA?请教个算法加编程
讨论 找单链表倒数m的节点 (转载)请问一个算法
问题请教请教 C++ 题
问个树遍历的线程化问题求购 书 算法
[合集] 二叉树的实现提议:发起写书评活动
求教:根据给定数组创建二叉树Data strctures方面的好书?
相关话题的讨论汇总
话题: tree话题: cormen话题: stored话题: binary话题: space
进入Programming版参与讨论
1 (共1页)
i***h
发帖数: 12655
1
这个是那本算法书二叉树一节后的题
网上查了下,除非节点带对父节点的指针才行,好象作弊了(这个额外的指针就是O(n)空
间了)
有答案么?
谢谢
c*****t
发帖数: 1879
2
If the tree can be modified...
/ \ are downward arrows, * is the upward arrow, stored in either left
or right branch.
root := A
A
/ \
B C
/ \
D E
1.
A
* \
B C
/ \
D E
2.
A
* \
B C
* \
D E
3. then backup and visit E
A
* \
B C


【在 i***h 的大作中提到】
: 这个是那本算法书二叉树一节后的题
: 网上查了下,除非节点带对父节点的指针才行,好象作弊了(这个额外的指针就是O(n)空
: 间了)
: 有答案么?
: 谢谢

i***h
发帖数: 12655
3
你问到点子上了
要求是不能
[Cormen习题11.4-5] O(n) time, non-recursive, constant extra space, do not
modify the tree (even temporarily)
很刁难人啊

【在 c*****t 的大作中提到】
: If the tree can be modified...
: / \ are downward arrows, * is the upward arrow, stored in either left
: or right branch.
: root := A
: A
: / \
: B C
: / \
: D E
: 1.

c*****t
发帖数: 1879
4
那么题目出错了 :)
100% 可以肯定。

【在 i***h 的大作中提到】
: 你问到点子上了
: 要求是不能
: [Cormen习题11.4-5] O(n) time, non-recursive, constant extra space, do not
: modify the tree (even temporarily)
: 很刁难人啊

l******u
发帖数: 1174
5
你肯定你看见了整个题目了?
就这个?
lz把原题找出来我们再下结论吧.
i***h
发帖数: 12655
6
Introduction to Algorithms
1990 MIT Press
Page 216
11.4-5 *
Write an O(n)-time nonrecursive procedure that, given an n-node binary tree,
prints out the key of each node. Use no more than constant extra space
outside of the tree itself and do not modify the tree, even temporarily,
during the procedure.
n******t
发帖数: 4406
7
哈哈,同学你被晃点了,CLRS那本书里面的binrary tree每个节点是带了
指向parent节点的指针的。

【在 i***h 的大作中提到】
: 这个是那本算法书二叉树一节后的题
: 网上查了下,除非节点带对父节点的指针才行,好象作弊了(这个额外的指针就是O(n)空
: 间了)
: 有答案么?
: 谢谢

l******u
发帖数: 1174
8
The problem doesn't specify how the binary tree is stored, and whether the
traversal has to be in certain order. These are the two areas you can
expand on. If the solution has to work for any binary tree implementation
and any traversal order, I think it simply does not exist.
You can certainly store a binary tree in a way so that it can be traversed
in O(n) time + O(1) space. Parent pointer is one simply way. Storing in a
continuous array is another. Or you can use sibling pointers. Etc.
In

【在 i***h 的大作中提到】
: Introduction to Algorithms
: 1990 MIT Press
: Page 216
: 11.4-5 *
: Write an O(n)-time nonrecursive procedure that, given an n-node binary tree,
: prints out the key of each node. Use no more than constant extra space
: outside of the tree itself and do not modify the tree, even temporarily,
: during the procedure.

i***h
发帖数: 12655
9
一语惊醒梦中人
原来不是我傻
而是我傻透了
多谢大家的讨论和指点

【在 n******t 的大作中提到】
: 哈哈,同学你被晃点了,CLRS那本书里面的binrary tree每个节点是带了
: 指向parent节点的指针的。

i***h
发帖数: 12655
10
Thanx

binary
node.

【在 l******u 的大作中提到】
: The problem doesn't specify how the binary tree is stored, and whether the
: traversal has to be in certain order. These are the two areas you can
: expand on. If the solution has to work for any binary tree implementation
: and any traversal order, I think it simply does not exist.
: You can certainly store a binary tree in a way so that it can be traversed
: in O(n) time + O(1) space. Parent pointer is one simply way. Storing in a
: continuous array is another. Or you can use sibling pointers. Etc.
: In

1 (共1页)
进入Programming版参与讨论
相关主题
Data strctures方面的好书?问题请教
BFS traversal starting from leaf level ???问个树遍历的线程化问题
Any big changes to 1991版Introduction to Algorithms (Cormen, Leiserson, Rivest) ?[合集] 二叉树的实现
二叉树 (转载)求教:根据给定数组创建二叉树
按层遍历二叉树,常量空间,如何做到?面题:copy directed graph
今天面了个老印[合集] 给定一个最小堆,如何查找某数是否存在此堆中?
怎么用lex处理DFA?请教个算法加编程
讨论 找单链表倒数m的节点 (转载)请问一个算法
相关话题的讨论汇总
话题: tree话题: cormen话题: stored话题: binary话题: space