注:本博客参考了牛客网视频解法
对二叉树进行广度优先遍历,且进行逐层打印
请用递归和非递归的方式分别实现二叉树的先中后序遍历打印
构建 node 类
class Node(object):
def __init__(self,item):
self.item = item
self.left = None
self.right = None
构建树
class Tree(object):
def __init__(self):
self.head = None
实现添加元素
def add(self,item):
node = Node(item)
if self.head == None:
self.head = node
return
queuen = [self.head]
while queuen:
a = queuen.pop(0)
if a.left == None:
a.left = node
return
else:
queuen.append(a.left)
if a.right == None:
a.right = node
return
else:
queuen.append(a.right)
不进行换行
def travel(self):
queuen = [self.head]
while queuen:
a = queuen.pop(0)
print(a.item)
if a.left != None:
queuen.append(a.left)
if a.right !=None:
queuen.append(a.right)
逐层打印
应用queue里的数据是一层一层存储的,可通过计数进行控制
可设置计数装置进行深度统计
def layer1(self):
out_list = []
queue = [self.head]
count = 0
while queue:
res = []
i = 0
number = len(queue)
while i < number:
cur = queue.pop(0)
res.append(cur.item)
if cur.left != None:
queue.append(cur.left)
if cur.right != None:
queue.append(cur.right)
i += 1
count +=1
print(res)
out_list.append(res)
print(count)
return out_list
设置新的List进行逐层存储然后将每一层分别赋值给原先的层即可
可设置计数装置进行深度统计
def layer2(self):
queue = [self.head]
out_list = []
while queue:
new_queue = []
res = []
num = len(queue)
i = 0
while i < num:
cur = queue.pop(0)
res.append(cur.item)
if cur.left != None:
new_queue.append(cur.left)
if cur.right != None:
new_queue.append(cur.right)
i +=1
queue = new_queue
print(res)
out_list.append(res)
return out_list
递归
def pre_travel(self,node):
if self.head == None:
return
print(node.item,end=" “)
self.pre_travel(node.left)
self.pre_travel(node.right)
非递归: 通过设置栈先将右节点压入再将左节点压入,再弹出遍历
def pre_travel2(self,node):
stack = [node]
while len(stack) > 0:
cur = stack.pop()
print(cur.item,end=" ")
if cur.right != None:
stack.append(cur.right)
if cur.left != None:
stack.append(cur.left)
递归
def mid_travel(self,node):
if self.node == None:
return
self.mid_travel(node.left)
print(node.item,end=" ")
self.mid_travel(node.right)
非递归:通过设置栈先将左节点压入栈直到左节点为空,再弹出栈顶,打印当前元素,并将当前节点的右孩子赋值给cur,依次循环。
def mid_travel2(self,node):
stack = []
cur = node
if cur == None :
return
while len(stack)>0 or cur!=None :
while cur != None:
stack.append(cur)
cur = cur.left
if len(stack)>0:
a1 = stack.pop()
cur = a1.right
print(a1.item,end=" ")
递归
def last_travel(self,node):
if node == None:
return
self.last_travel(node.left)
self.last_travel(node.right)
print(node.item,end=" ")
非递归:用两个栈 s2 存储遍历的元素
def last_travel2(self,node):
s1 = []
s2 = []
s1 = [node]
while len(s1)>0:
cur = s1.pop()
s2.append(cur.item)
if cur.left != None:
s1.append(cur.left)
if cur.right != None:
s1.append(cur.right)
while len(s2)>0 :
print(s2.pop(),end=" ")
空树是平衡二叉树
如果一颗树不为空,并且其中所有的子树都满足各自的左子树与右子树的高度差都不超过1.
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
获取左右子树的深度
左右的深度相减,若差值大于一则返回False
对所有节点进行递归判断。
判断树的深度
def get_deepth(self,node):
if node == None:
return 0
nright = self.get_deepth(node.right)
nleft = self.get_deepth(node.left)
return max(nright,nleft)+1
判断是否是平衡二叉树
def isbalance(self,node):
if node == None:
return True
right = self.get_deepth(node.right)
left = self.get_deepth(node.left)
if abs(right-left) >1:
return False
a = self.isbalance(node.right)
b = self.isbalance(node.left)
return a and b
每个子树的头结点的值都比各自的左子树上的所有节点的值大,也比各自右子树上的所有节点的值要小。
搜索二叉树按照中序遍历得到的序列一定是从小到大排列的。
红黑树、平衡搜索二叉树(AVL树)等,其实都是搜索二叉树的不同实现。都力争使搜索的效率尽量提高,调整的代价减小。
给定一颗二叉树的头结点,判断这棵树是否是搜索二叉树
改写中序遍历,判断后一个值一定比上一个值大
def is_search_tree(self,node):
stack = []
res = []
cur = node
if cur == None:
return True
while len(stack) > 0 or cur != None:
while cur != None:
stack.append(cur)
cur = cur.left
if len(stack) > 0:
a1 = stack.pop()
cur = a1.right
if len(res) == 0:
pass
else:
if a1.item < res[-1]:
return False
res.append(a1.item)
return True