#! /usr/bin/env python #coding:utf-8 from collections import namedtuple from sys import stdout Node = namedtuple('Node', 'data, left, right') tree = Node(1, Node(2, Node(4, Node(7, None, None), None), Node(5, None, None)), Node(3, Node(6, Node(8, None, None), Node(9, None, None)), None)) #前序(NLR) def preorder(node): if node is not None: print node.data, preorder(node.left) preorder(node.right) #中序(LNR) def inorder(node): if node is not None: inorder(node.left) print node.data, inorder(node.right) #后序(LRN) def postorder(node): if node is not None: postorder(node.left) postorder(node.right) print node.data, #层序(levelorder) def levelorder(node, more=None): if node is not None: if more is None: more = [] more += [node.left, node.right] print node.data, if more: levelorder(more[0], more[1:]) if __name__=="__main__" print ' preorder: ', preorder(tree) print '\t\n inorder: ', inorder(tree) print '\t\n postorder: ', postorder(tree) print '\t\nlevelorder: ', levelorder(tree) print '\n'
源码请到我的github中的algorithm查找,文件名为:binary_tree_traversal.py。
原文:http://blog.csdn.net/qiwsir/article/details/37744171