转换二叉树preorder上市后序,反之亦然 [英] Convert Preorder listing of a binary tree to postorder and vice versa

查看:129
本文介绍了转换二叉树preorder上市后序,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能找到一棵树的preorder上市如果只是后序上市,并给出反之亦然。此外,在树中,每个非叶结点有两个孩子(即每个节点有两个或零儿童。)

How can I find the preorder listing of a tree if only the postorder listing is given and vice versa. Also, in the tree, every non-leaf node has two children (i.e. Each node has either two or zero children.)

编辑:另一个给定的假设是,各节点的标签是唯一的并具有将其标识为内部节点或叶的字段。我认为,应该摆脱单一preorder的模糊性或后序能够唯一标识一棵树。

Another given assumption is that the label of each node is unique and has a field that will identify it as an internal node or a leaf. I think that ought to get rid of the ambiguity of a single preorder or postorder being able to uniquely identify a tree.

推荐答案

在不假设树的节点有idenrify自己内部或叶的字段,你找不到你的问题了独特的答案。这种假设或序房源必须是可用找到一种独特的树。 在这种情况下,找到一个可能的答案,你可以构造如下图所示,以匹配任何给定的后序上市形式的树:(假设后序列表:1 2 3 4 5 6 7 8 9)

Without assuming that nodes in the tree have a field that idenrify themselves as internal or leaf, you can't find a unique answer for your question. That assumption or inorder listing must be available to find a unique tree. In this case, to find one possible answer, you can construct a tree of the form shown below to match any given postorder listing: (suppose postorder listing is: 1 2 3 4 5 6 7 8 9)

9[7[5[3[1,2],4],6],8]

现在你可以让preorder使用这个树状列表。

Now you can make preorder listing using this tree.

假设树的节点有自己的身份为内部或叶一个领域,我们可以做一个独特的树了后序上市对于这种用这种算法树:

Assuming nodes in the tree have a field that identify themselves as internal or leaf, we can make a unique tree out of a postorder listing for this kind of trees with this algorithm:

  1. 扫描从后序列表的开头和找到的第一个内部节点。该节点将有整整两个叶片孩子其中precede在后序上市这一节点。
  2. 在树形结构添加内部节点,并提出两点preceding节点上市的孩子。
  3. 从列表中删除这两个孩子,使该内部节点叶。
  4. 转到步骤1,重复直到上市变为空。

这篇关于转换二叉树preorder上市后序,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆