pyprog 0 Light Poster

I am wondering how to write a function that prints values of tree nodes from a root to a lowest child. If the tree looks like

1
  2          3
4   5      6   7
         8

then the output should be 1 3 6 8. My function is

def traverse(node):
    if node is None:
        return 0
    else:
        print node.value
	traverse(node.left)
	traverse(node.right)

but it prints every value of every node. I can't understand how to 'guide' a function along the specific path in the tree. Thanks for the help in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.