Python实现二叉树的遍历
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python实现二叉树的遍历相关的知识,希望对你有一定的参考价值。
Python实现二叉树的遍历
class BinaryTree(object): def __init__(self, value=None, left=None, right=None): self.value = value self.left = left self.right = right def rebuild(self, preOrder, inOrder): """ 根据前序列表和中序列表,重建二叉树 :param preOrder: 前序列表 :param inOrder: 中序列表 :return: 二叉树 """ if preOrder == None or inOrder == None or len(preOrder) <=0 or len(inOrder) <=0 or len(preOrder) != len(inOrder): return None cur = BinaryTree(preOrder[0]) index = inOrder.index(preOrder[0]) cur.left = self.rebuild(preOrder[1: index+1], inOrder[:index]) cur.right = self.rebuild(preOrder[index+1:], inOrder[index+1:]) return cur def preOrder(self, tree): """ 前序遍历 :param tree: :return: """ if tree == None: return None print(tree.value, end=‘ ‘) self.preOrder(tree.left) self.preOrder(tree.right) def preOrderLoop(self, tree): """ 前序遍历的循环实现 :param tree: :return: """ if tree == None: return None lst = [] node = tree while node != None or len(lst) > 0: if node != None: lst.append(node) print(node.value, end=‘ ‘) node = node.left else: item = lst[len(lst)-1] lst = lst[:-1] node = item.right def inOrder(self, tree): """ 中序遍历 :param tree: :return: """ if tree == None: return None self.inOrder(tree.left) print(tree.value, end=‘ ‘) self.inOrder(tree.right) def inOrderLoop(self, tree): """ 中序遍历循环实现 :param tree: :return: """ if tree == None: return lst = [] node = tree while node != None or len(lst) > 0: if node != None: lst.append(node) node = node.left else: item = lst[len(lst)-1] lst = lst[:-1] print(item.value, end=‘ ‘) node = item.right def postOrder(self, tree): """ 后序遍历 :param tree: :return: """ if tree == None: return None self.postOrder(tree.left) self.postOrder(tree.right) print(tree.value, end=‘ ‘) def postOrderLoop(self, tree): """ 后续遍历的循环实现 :param tree: :return: """ if tree == None: return None visited = set() lst = [] node = tree while node != None or len(lst) > 0: if node != None: lst.append(node) node = node.left else: item = lst[len(lst)-1] if item.right != None and item.right not in visited: node = item.right else: print(item.value, end=‘ ‘) visited.add(item) lst = lst[:-1]
本文出自 “许大树” 博客,请务必保留此出处http://abelxu.blog.51cto.com/9909959/1968898
以上是关于Python实现二叉树的遍历的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode第144题—二叉树的前序遍历—Python实现