我有一棵二叉树,我需要获得叶子和根之间的所有序列 例如,对于这样的树
我需要得到序列:“ABD”,“ABE”,“AC” 怎么实现呢?谢谢。
答案 0 :(得分: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, "")