java - 一般树的后序遍历

标签 java tree binary-tree tree-traversal postorder

我目前是一名学生,作业涉及将二叉树方法改编为通用树方法。 我唯一的问题是, 我对以下通用树的后序遍历是否正确? 如果是这样,那么我就知道我的算法正在运行,我只是无法正确掌握后序遍历的窍门,我觉得并认为该网站可以提供帮助。

                     B
--------------------|------------------
|                   |                 |
A             ------D-----         ---I---
             |      |    |        |       |
             C      E    G        H       L
                         |
                         F

我的结果是: A C E F G D H L I B

最佳答案

我觉得你的回答是正确的。

这个技巧适用于广义树,而不仅仅是二元树。

沿着虚线走,找到黑点就访问节点。

Post order traversal

重用这张图进行前序遍历,只需要将所有黑点旋转 180 度即可。中序遍历将是 90 度,但如果这不是二叉树,则不明确。

参见 http://en.wikipedia.org/wiki/Tree_traversal

来自 http://www.brpreiss.com/books/opus4/html/page259.html

postorder traversal visits the root last. To do a postorder traversal of a general tree:

Do a postorder traversal each of the subtrees of the root one-by-one in the order given; and then visit the root.

关于java - 一般树的后序遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581380/

相关文章:

java - 如何防止抛出异常时用户输入被结转?

java - 点击时无法触发

java - C :url not appending jsessionid when cookies are disabled

tree - 用于折叠树分支的 SPARQL 查询(汇总拓扑)

algorithm - 查找二叉树中最大独立集的大小 - 为什么错误的 "solution"不起作用?

Java API的newInstance和OSGi

algorithm - 如果树是平衡的,在二叉搜索树中搜索的时间复杂度是多少?

algorithm - 反转数组查询

c++ - 为二叉树重载++ 运算符

c - 二叉树,C语言