从下至上扫描树结构? [英] Scan tree structure from bottom up?

查看:100
本文介绍了从下至上扫描树结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给出以下树状结构或类似的树状结构:

If given the following tree structure or one similar to it:

我希望返回字符串ZYXWVUT。我知道如何用一棵二叉树来做到这一点,但没有一个可以拥有超过子节点的树。任何帮助将不胜感激。

I would want the string ZYXWVUT returned. I know how to do this with a binary tree but not one that can have more than child nodes. Any help would be much appreciated.

推荐答案

这称为树的后遍历:在打印节点本身的内容之前,先打印树的所有子树的内容。

This is called a post-order traversal of a tree: you print the content of all subtrees of a tree before printing the content of the node itself.

这可以递归完成,就像这样(伪代码):

This can be done recursively, like this (pseudocode):

function post_order(Tree node)
    foreach n in node.children
        post_order(n)
    print(node.text)

这篇关于从下至上扫描树结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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