
描述
Given a binary tree, flatten it to a linked list in-place.
For example,
Given1 / 2 5 / 3 4 6The flattened tree should look like:
12
3
4
5
6
Hints:
If you notice carefully in the flattened tree, each node’s right child points to the next node of a pre-order traversal.
分析
这道题有点莫名其妙,不知道初衷是什么。按照提示,先序遍历,把每个被访问的节点右儿子设为下一个要访问的节点。迭代法用栈实现,时间O(n),空间O(logn)。
代码
Python
1 |
class (object): |




近期评论