Click to See Complete Forum and Search --> : binary tree traversal left to right leaves to root


chickenandfries
January 31st, 2008, 04:32 AM
Is there an algorithm that traverses a tree left to right from the leaves to the root? It's like reverse level order traversal but instead of right to left the traversal would be left to right. If there is no such algorithm, I would appreciate any suggestion, at least an outline or a pseudocode, of how I could do that. An algorithm of reverse level order traversal might help and would be greatly appreciated too.

CMPITG
February 4th, 2008, 01:50 AM
You should use postorder traversal:
visit(u):
for each v in Child(u):
visit(v)
print u

chickenandfries
February 10th, 2008, 01:35 AM
Thanks. Didn't think it was that simple.

CMPITG
February 10th, 2008, 11:56 AM
You're welcome :D