我得到了一棵严格二叉树的后序遍历,并被要求找出它的前序遍历。通常,我会首先构建这棵树,然后找到前序遍历。但是,我想知道是否有一种方法可以在不实际构建树的情况下找到前序遍历。
我得到了一棵严格二叉树的后序遍历,并被要求找出它的前序遍历。通常,我会首先构建这棵树,然后找到前序遍历。但是,我想知道是否有一种方法可以在不实际构建树的情况下找到前序遍历。
[编辑: 我最初在假设给定的后序遍历是严格有序的二叉搜索树的情况下回答了这个问题。OP现在指出了我的错误并提供了一个示例。然而,两种算法的基本原理是相同的:找到左子树和右子树之间的边界。]
让我们考虑以下完全二叉树的后序遍历,其中每个节点都是叶子节点或具有两个叶子节点的支路:
"1, 2, B, 3, 4, D, 5, C, A
int is_leaf(int c)
{
return !isalpha(c); // non-alphas are leaves
}
void reconst(int **pre, const int *post, int lo, int hi)
{
printf("%d :: %d\n", lo, hi);
if (lo < hi) {
int k = --hi; // will be boundary between l/r
int root = post[k];
int leaves = 0;
int nodes = 0;
while (k > lo && nodes != 2 * leaves - 1) {
if (is_leaf(post[k - 1])) leaves++;
nodes++;
k--;
}
*(*pre)++ = root;
reconst(pre, post, lo, k);
reconst(pre, post, k, hi);
}
}
像这样调用:
int post[] = {'1', '2', 'B', '3', '4', 'D', '5', 'C', 'A'};
int n = 9;
int pre[9];
int *p = pre;
int i;
reconst(&p, post, 0, n);
for (i = 0; i < n; i++) {
printf("%c ", pre[i]);
}
puts("pre");
pre
数组必须与post
数组一样大,以容纳重建后的前序遍历。(b) 输入必须是良好形式的。该算法依赖于找到正确的完整子树。(计数器受到越过下限的保护,但仅此而已。)
[原始帖子试图从二叉搜索树的后序遍历中找到没有重复值的预订,即具有严格顺序。不错的答案,但由于误解了要求,不是OP想要的。对此感到抱歉。]
假设您得到一个如下的后序遍历:
3, 1, 7, 9, 8, 5
void reconst(int **pre, const int *post, int lo, int hi)
{
if (lo < hi) {
int k = --hi; // k will be the boundary between l/r
int parent = post[k]; // remove parent from this subarray
// find boundary between left and right branches
while (k > lo && post[k - 1] > parent) k--;
*(*pre)++ = parent; // write parent to pre-order array
reconst(pre, post, lo, k); // do the left subarray
reconst(pre, post, k, hi); // do the right subarray
}
}
pre数组通过指向指针的指针填充:顶层指针跟踪
pre
数组中的位置,第二级指针访问底层数组。(如果这太复杂了,您可以传递一个数组索引,然后将其推进。)
像这样调用函数:
int post[] = {3, 1, 7, 9, 8, 5};
int n = 6;
int pre[6];
int *p = pre;
int i;
reconst(&p, post, 0, n);
for (i = 0; i < n; i++) {
printf("%d ", pre[i]);
}
puts("pre");
如果您假设任何二叉树,而不是二叉搜索树,那么我认为不可能获得唯一的前序遍历序列。原因在于,您无法知道后序遍历序列中第二个、第三个等节点是左子树还是右子树的根节点。如果树是二叉搜索树,则很清楚,因为顺序将告诉您节点是在左子树还是右子树中。
如果您只想要一个与给定后序遍历序列对应的前序遍历序列,则可以假设一个从左侧不平衡的树。因此,简单地反转后序遍历序列就是左侧不平衡树的前序遍历序列。
例如,如果您的后序遍历序列是"abcd"
,那么"dcba"
是该树的有效前序遍历序列:
d
/
c
/
b
/
a
也许不是你想要的答案,但我认为这回答了你的陈述。