How can I write a non recursive algorithm to add root to tree?

68 views Asked by At

I need a non recursive algorithm that will add up the root to the tree values, and then display the value that is the highest. Not to add up every element in the tree, just the highest value way to get from root to leaf.

                        2
                     /      \
                    8         6

in this example the answer would be 10 must be in O(n) timing

1

There are 1 answers

0
yacc On

Try it breadth-first. Add the children of the actual node to a list, then proceed along that list until you reach its end.