遍历二叉树

递归实现

前序遍历

# Tree Class
class TreeNode(object):
	def __init__(self, data, left = None, right = None):
		self.data = data
		self.left = left
		self.right = right
	def __str__(self):
		return str(self.data)	
# Create a tree
def createTree():
	A, B, C, D, E, F, G, H, I = [TreeNode(_) for _ in "ABCDEFGHI"]
	A.left = B 
    A.right = C 
    B.right = D 
    C.left = E 
    C.right = F 
    E.left = G 
    F.left = H 
    F.right = I
    return A
def preOrder(root):		
	if not root:
		return
	result.append(root)
	preOrder(root.left)
	preOrder(root.right)
if __name__ == '__main__':
	root = createTree()
	result = []
	preOrder(root)
	for i in range(len(result)):
		print(result[i])

中序遍历

# Tree Class
class TreeNode(object):
	def __init__(self, data, left = None, right = None):
		self.data = data
		self.left = left
		self.right = right
	def __str__(self):
		return str(self.data)	
# Create a tree
def createTree():
	A, B, C, D, E, F, G, H, I = [TreeNode(_) for _ in "ABCDEFGHI"]
	A.left = B 
    A.right = C 
    B.right = D 
    C.left = E 
    C.right = F 
    E.left = G 
    F.left = H 
    F.right = I
    return A
def inOrder(root):		
	if not root:
		return
	inOrder(root.left)
	result.append(root)
	inOrder(root.right)
if __name__ == '__main__':
	root = createTree()
	result = []
	inOrder(root)
	for i in range(len(result)):
		print(result[i])

后序遍历

# Tree Class
class TreeNode(object):
	def __init__(self, data, left = None, right = None):
		self.data = data
		self.left = left
		self.right = right
	def __str__(self):
		return str(self.data)	
# Create a tree
def createTree():
	A, B, C, D, E, F, G, H, I = [TreeNode(_) for _ in "ABCDEFGHI"]
	A.left = B 
    A.right = C 
    B.right = D 
    C.left = E 
    C.right = F 
    E.left = G 
    F.left = H 
    F.right = I
    return A
def postOrder(root):		
	if not root:
		return
	postOrder(root.left)
	postOrder(root.right)
	result.append(root)
if __name__ == '__main__':
	root = createTree()
	result = []
	postOrder(root)
	for i in range(len(result)):
		print(result[i])

循环实现

前序遍历

# 中左右
def preOrder(root):
	node = root
	stack = []
	result = []
	while node or stack:
		if node:
			result.append(node)
			stack.append(node.right)
			node = node.left
		else:
			node = stack.pop()
	return result

中序遍历

# 左中右
def inOrder(root):
	node = root
	stack = []
	result = []
	while node or stack:
		if node:
			stack.append(node)
			node = node.left
		else:
			node = stack.pop()
			result.append(node)
			node = node.right
		return result

后序遍历

# 中右左,反转变为左右中
def postOrder(root):
	node = root
	stack = []
	result = []
	while node or stack:
		if node:
			result.append(node)
			stack.append(node.left)
			node = node.right
		else:
			node = stack.pop()
	result.reverse()
	return result
			

层序遍历(用队列实现)

from collections import deque
def levelOrder(root):
	q = deque([root])
	result = []
	while q:
		node = q.popleft()
		result.append(node)
		if node.left:
			q.append(node.left)
		if node.right:
			q.append(node.right)
	for i in range(len(result)):
		print(result[i])
	return result
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值