This code is to implement the traversal of the binary tree
层序 先序 中序 后序
class Node:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Tree:
def __init__(self):
self.root = None
def add(self, x):
node = Node(x)
if self.root is None:
self.root = node
else:
q = [self.root]
while True:
pop_node = q.pop(0)
if pop_node.left is None:
pop_node.left = node
return
elif pop_node.right is None:
pop_node.right = node
return
else:
q.append(pop_node.left)
q.append(pop_node.right)
def traverse(self): # 层次遍历
if self.root is None:
return None
q = [self.root]
res = [self.root.val]
while q != []:
pop_node = q.pop(0)
if pop_node.left is not None:
q.append(pop_node.left)
res.append(pop_node.left.val)
if pop_node.right is not None:
q.append(pop_node.right)
res.append(pop_node.right.val)
return res
def preorder(self,root): # 先序遍历
if root is None:
return []
result = [root.val]
left_val = self.preorder(root.left)
right_val = self.preorder(root.right)
return result + left_val + right_val
def inorder(self,root): # 中序序遍历
if root is None:
return []
result = [root.val]
left_val = self.inorder(root.left)
right_val = self.inorder(root.right)
return left_val + result + right_val
def postorder(self,root): # 后序遍历
if root is None:
return []
result = [root.val]
left_val = self.postorder(root.left)
right_val = self.postorder(root.right)
return left_val + right_val + result
t = Tree()
for i in range(10):
t.add(i)
print('层序遍历:',t.traverse())
print('先序遍历:',t.preorder(t.root))
print('中序遍历:',t.inorder(t.root))
print('后序遍历:',t.postorder(t.root))
The result of the operation is