Skip to content

Instantly share code, notes, and snippets.

@caoxudong
Created January 26, 2015 03:56
Show Gist options
  • Save caoxudong/45406c3287796c88a80a to your computer and use it in GitHub Desktop.
Save caoxudong/45406c3287796c88a80a to your computer and use it in GitHub Desktop.
树深度优先搜索
#!/usr/bin/env python
__author__ = 'caoxudong'
from datastructure.common_datastructure import node
def visit_node(node):
print(node.data)
def depth_first_search(root=node.Node):
if root:
stack = []
stack.append(root)
while len(stack):
temp_node = stack.pop()
visit_node(temp_node)
if temp_node.children and len(temp_node.children) > 0:
for e in temp_node.children:
stack.append(e)
if __name__ == '__main__':
root = node.Node
depth_first_search(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment