0

I have a binary tree and I need to get all sequences between the leaves and the root.
For example, for such tree
tree

I need to get the sequences: "ABD", "ABE", "AC".
How to implement it? Thanks.

Vladimir G.
  • 885
  • 7
  • 15
  • You should be more precise about what you really want to achieve – pjam Nov 13 '12 at 12:20
  • @pjam: user wants all possible paths from the root node to the leaves – das_weezul Nov 13 '12 at 12:27
  • I have a binary tree(not BST or binary heap). Each node contains some value(A, B, C... in example above). As a result, I need to get something like: String[leaves_count] seq = ["ABD", "ABE", "AC"]. Thanks for the answer. – Vladimir G. Nov 13 '12 at 12:32

1 Answers1

1

Pseudo code :

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, "")
Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53