Monday, April 16, 2012

Breadth First Traversal for a Tree, Python

I figured out Depth First Traversal for a Tree.



def _dfs(tree, res):
if tree:
res += [tree.key]
_dfs(tree.left, res)
_dfs(tree.right, res)
return res


I can't seem to find a solution for Breadth First Search. Will one have to use queues or stacks?



Thanks!!





No comments:

Post a Comment