【发布时间】:2017-03-24 15:50:18
【问题描述】:
我能否仅从一个排序为后序遍历的数组中恢复整个二叉树 (count(vertices) = 2^n-1)?
我建议的算法很简单,就是做后序遍历:
- 向左走
- 向右走
- current_vertex = 数组[i++]; // 初始化 i = 0
应该可以完成这项工作,不是吗?
【问题讨论】:
标签: binary-tree binary-search-tree postorder
我能否仅从一个排序为后序遍历的数组中恢复整个二叉树 (count(vertices) = 2^n-1)?
我建议的算法很简单,就是做后序遍历:
应该可以完成这项工作,不是吗?
【问题讨论】:
标签: binary-tree binary-search-tree postorder
不,你不能因为binary tree 中的post order array 可以转换为多个二叉树,它们不相同,因此无法知道哪一棵是原始的。
例子-
Post order'd 数组 2 3 5 可以转换为许多树,例如-
2 // 3 is right child of 2 and 5 of 5
3
5
或
5 //2 is left child and 3 is right child of 5
2 3
【讨论】: