Originally posted by Div Bala:
Can you help me with the traversal part? Again should I write a recursive function?
Yes
you should.
Initially path is an empty list when you call it with the root node.
What if there are two root nodes say like
A - C , D
B - C , D
Then you have to call the method(s) for both nodes.
All recursive tree operations have two assumptions:
1) there is one root
2) there are no cycles
1 because you always start at the root, and 2 because cycles could (will) cause your code to run forever - until you get either a StackOverFlowError or an OutOfMemoryError.