interactive-coding-challenges/graphs_trees/bst/dfs.py

17 lines
604 B
Python
Raw Normal View History

2015-08-02 05:44:14 +08:00
def in_order_traversal(node, visit_func):
if node is not None:
in_order_traversal(node.left, visit_func)
visit_func(node.data)
in_order_traversal(node.right, visit_func)
def pre_order_traversal(node, visit_func):
if node is not None:
visit_func(node.data)
pre_order_traversal(node.left, visit_func)
pre_order_traversal(node.right, visit_func)
def post_order_traversal(node, visit_func):
if node is not None:
post_order_traversal(node.left, visit_func)
post_order_traversal(node.right, visit_func)
visit_func(node.data)