树的构造
class TreeNode:
def __init__(self,val,left=None,right=None):
self.val=val
self.left=left
self.right=right
t7 = TreeNode(7)
t6 = TreeNode(6)
t5 = TreeNode(5)
t4 = TreeNode(4)
t3 = TreeNode(3,t6,t7)
t2 = TreeNode(2,t4,t5)
t1 = TreeNode(1,t2,t3)
二叉树的遍历
递归-先序遍历
res = []
def pre_order(root):
global res
if not root:
return
res.append(root.val)
pre_order(root.left)
pre_order(root.right)
pre_order(t1)
print("先序遍历:", res)
递归-中序遍历
res = []
def in_order(root):
global res
if not root:
return
in_order(root.left)
res.append(root.val)
in_order(root.right)
in_order(t1)
print("中序遍历:", res)
递归-后序遍历
res = []
def post_order(root):
global res
if not root:
return
post_order(root.left)
post_order(root.right)
res.append(root.val)
post_order(t1)
print("后序遍历:", res)
递归-层次遍历
def level_order(root):
def helper(root, lev):
if not root:
return
else:
sol[lev-1].append(root.val)
if len(sol) == lev:
sol.append([])
helper(root.left,lev + 1)
helper(root.right, lev + 1)
sol = [[]]
helper(root, 1)
return sol[:-1]
res = level_order(t1)
print("层次遍历:", res)
结果:
先序遍历: [1, 2, 4, 5, 3, 6, 7]
中序遍历: [4, 2, 5, 1, 6, 3, 7]
后序遍历: [4, 5, 2, 6, 7, 3, 1]
层次遍历: [[1], [2, 3], [4, 5, 6, 7]]
非递归-先序遍历
def pre_non_recursive_order(root):
if not root:
return []
res = []
stack = []
while stack or root:
while root:
res.append(root.val)
stack.append(root)
root = root.left
if stack:
tmp = stack.pop()
root = tmp.right
return res
res = pre_non_recursive_order(t1)
print("先序遍历:", res)
非递归-中序遍历
def in_non_recursive_order(root):
if not root:
return []
res = []
stack = []
while stack or root:
while root:
stack.append(root)
root = root.left
if stack:
tmp = stack.pop()
res.append(tmp.val)
root = tmp.right
return res
res = in_non_recursive_order(t1)
print("中序遍历:", res)
非递归-后序遍历
def post_non_recursive_order(root):
if not root:
return []
res = []
stack1 = [root]
stack2 = []
while stack1:
tmp = stack1.pop()
if tmp.left:
stack1.append(tmp.left)
if tmp.right:
stack1.append(tmp.right)
stack2.append(tmp)
while stack2:
res.append(stack2.pop().val)
return res
res = post_non_recursive_order(t1)
print("后序遍历:", res)
非递归-层次遍历
def level_non_recursive_order(root):
if not root:
return [[]]
res = []
queue = [root]
while queue:
tmp = []
for i in range(len(queue)):
t = queue.pop(0)
tmp.append(t.val)
if t.left:
queue.append(t.left)
if t.right:
queue.append(t.right)
res.append(tmp)
return res
res = level_non_recursive_order(t1)
print("层次遍历:", res)
结果:
先序遍历: [1, 2, 4, 5, 3, 6, 7]
中序遍历: [4, 2, 5, 1, 6, 3, 7]
后序遍历: [4, 5, 2, 6, 7, 3, 1]
层次遍历: [[1], [2, 3], [4, 5, 6, 7]]
二叉树的序列化
递归-先序遍历
def serialize(root):
res = []
def pre_order(root, res):
if not root:
res.append("#")
return
res.append(root.val)
pre_order(root.left, res)
pre_order(root.right, res)
pre_order(root, res)
return res
res = serialize(t1)
print("序列化:",res)
非递归-先序遍历
def serialize(root):
def pre_non_recursive_order(root):
if not root:
return []
res = []
stack = []
while stack or root:
while root:
res.append(root.val)
stack.append(root)
root = root.left
res.append("#")
if stack:
tmp = stack.pop()
root = tmp.right
res.append("#")
return res
res = pre_non_recursive_order(root)
return res
res = serialize(t1)
print("序列化:",res)
结果:
序列化: [1, 2, 4, '#', '#', 5, '#', '#', 3, 6, '#', '#', 7, '#', '#']
递归-后序遍历
def serialize(root):
res = []
def post_order(root):
if not root:
res.append("#")
return
post_order(root.left)
post_order(root.right)
res.append(root.val)
return res
res = post_order(root)
return res
res = serialize(t1)
print("序列化:",res)
非递归-后序遍历
def serialize(root):
res = []
def post_non_recursive_order(root):
if not root:
return []
stack1 = [root]
stack2 = []
while stack1:
tmp = stack1.pop()
stack2.append(tmp)
if tmp.left:
stack1.append(tmp.left)
else:
stack2.append(TreeNode("#"))
if tmp.right:
stack1.append(tmp.right)
else:
stack2.append(TreeNode("#"))
while stack2:
res.append(stack2.pop().val)
return res
res = post_non_recursive_order(root)
return res
res = serialize(t1)
print("序列化:",res)
结果:
序列化: [4, '#', '#', 5, '#', '#', 2, 6, '#', '#', 7, '#', '#', 3, 1]
递归-层次遍历
def serialize(root):
res = [[]]
def level_order(root,lev):
if not root:
res[lev-1].append("#")
return
else:
res[lev-1].append(root.val)
if len(res) == lev:
res.append([])
level_order(root.left,lev+1)
level_order(root.right,lev+1)
level_order(root, 1)
return res
res = serialize(t1)
print("序列化:",res)
非递归-层次遍历
def serialize(root):
def level_non_recursive_order(root):
res = []
queue = [root]
while queue:
tmp = []
for _ in range(len(queue)):
t = queue.pop(0)
if t is None:
tmp.append("#")
continue
tmp.append(t.val)
queue.append(t.left)
queue.append(t.right)
res.append(tmp)
return res
res = level_non_recursive_order(root)
return res
res = serialize(t1)
print("序列化:",res)
结果:
序列化: [[1], [2, 3], [4, 5, 6, 7], ['#', '#', '#', '#', '#', '#', '#', '#']]
二叉树的反序列化
递归-先序遍历
def deserialize(arr):
if len(arr) == 0:
return None
val = arr.pop(0)
if val == "#":
return None
root = TreeNode(val)
root.left = deserialize(arr)
root.right = deserialize(arr)
return root
递归-后序遍历
def deserialize(arr):
if len(arr) == 0:
return None
val = arr.pop()
if val == "#":
return None
root = TreeNode(val)
root.left = deserialize(arr)
root.right = deserialize(arr)
return root
结果:
反序列化-层次遍历: [[1], [2, 3], [4, 5, 6, 7]]
层次遍历
def deserialize(arr):
def level_non_recursive_order(arr):
if len(arr) == 0:
return None
root = TreeNode(arr[0])
queue = [root]
i = 1
while queue:
parrent = queue.pop(0)
if arr[i] != "#":
parrent.left = TreeNode(arr[i])
queue.append(parrent.left)
else:
parrent.left = None
i+=1
if arr[i] != "#":
parrent.right = TreeNode(arr[i])
queue.append(parrent.right)
else:
parrent.right = None
i+=1
return root
root = level_non_recursive_order(arr)
return root
from itertools import chain
arr = list(chain.from_iterable(res))
root = deserialize(arr)
res2 = level_non_recursive_order(root)
print("反序列化-层次遍历:", res2)