0

我有一棵二叉树,我需要获取叶子和根之间的所有序列。
例如,对于这样的树
树

我需要得到序列:“ABD”、“ABE”、“AC”。
如何实施?谢谢。

4

1 回答 1

1

伪代码:

Function ProcessNode(TreeNode, ParentPath)
  CurrentPath = Append(ParentPath, TreeNode.Name)
  If IsNull(TreeNode.Left) And IsNull(TreeNode.Right) Then
    Print(CurrentPath)
  Else
    If IsNotNull(TreeNode.Left) Then ProcessNode(TreeNode.Left, CurrentPath)
    If IsNotNull(TreeNode.Right) Then ProcessNode(TreeNode.Right, CurrentPath)


ProcessNode(Root, "")
于 2012-11-13T12:35:33.160 回答