简体   繁体   English

后序遍历==自下而上遍历和前序遍历==自上而下遍历?

[英]Is post-order traversal == bottom-up traversal and pre-order traversal == top-down traversal?

Would it be right to say post-order traversal of a tree should be used to do a bottom-up traversal whereas a pre-order traversal should be used to do a top-down traversal of a binary tree?是否应该说应该使用树的后序遍历来进行自下而上的遍历,而应该使用前序遍历来进行二叉树的自上而下的遍历?

This has been true in all the examples that I have encountered, so I just wanted to confirm.在我遇到的所有示例中都是如此,所以我只是想确认一下。 There are some problems for which it is intuitive to start from the leaves (at the bottom).有些问题从叶子(底部)开始是直观的。 Can we use post-order traversal to solve them (and vice versa)?我们可以使用后序遍历来解决它们(反之亦然)吗?

Thanks!谢谢!

No. While post-order traversal and pre-order traversal have clear definition, bottom-up traversal or top-down traversal terms might be interpreted by different ways, they are not generally accepted for binary trees.不可以。虽然post-order traversalpre-order traversal有明确的定义,但bottom-up traversaltop-down traversal术语可能有不同的解释方式,但它们通常不被二叉树所接受。 If anybody uses last terms for trees, exact meaning depends on context.如果有人对树使用最后一个术语,那么确切的含义取决于上下文。

Look at the picture from wiki :看看维基上的图片:

在此处输入图像描述

Does pre-order traversal here look like top-down traversal ?这里的预购遍历看起来像top-down traversal吗? Or post-order likes bottom-up traversal ?还是后序喜欢bottom-up traversal Seems no.似乎没有。

Perhaps you want to consider BFS methods to get level order也许您想考虑 BFS 方法来获取级别顺序

No. And actually the terms bottom-up / top-down are generally used for graph, but for trees, it's sort of used as explained below:不。实际上,自下而上/自上而下的术语通常用于图形,但对于树,它的使用方式如下所述:

  • Preorder traversal: Parents are visited before children and siblings are visited in left-to-right order (might be successively but not always). Preorder traversal:先访问父母,然后再访问孩子,并按从左到右的顺序访问兄弟姐妹(可能依次但不总是)。
  • Postorder traversal: Children are visited before parents and siblings are visited in left-to-right order.后序遍历:在访问父母和兄弟姐妹之前访问孩子,按从左到右的顺序访问。
  • Top-down traversal: nodes are visited in the order of non-decreasing depth.自顶向下遍历:节点按深度不递减的顺序被访问。 Which is basically level-order traversal这基本上是水平顺序遍历
  • Bottom-up traversal: This is exactly reverse of top-down traversal自底向上遍历:这与自顶向下遍历完全相反

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM