节点类定义
class Node:
def __init__(self,val):
self.val=val
self.left=None
self.right=None
前序、中序、后序遍历的递归、非递归
#前序代码,中后续遍历,改变print的位置即可
def pre(root):
if not root:
return
print(root.val)
pre(root.left)
pre(root.right)
#前中续遍历代码,后序代码为前序向右遍历,再反向
def pre_(root):
stack=[]
node=root
while node or stack:
while node:
stack.append(node)
print(node.val)#前序
node=node.left
node=stack.pop()
#print(node.val) 中序代码
node=node.right
深度搜索DFS,递归与非递归
def depth_tree(root):
if root:
print(root.val)
if root.left:
return depth_tree(root.left)
if root.right:
return depth_tree(root.right)
def depth(root):
if not root:
return
stack=[root]
while stack:
a=stack.pop(0)
if a.right:
stack.insert(0,right)
if a.left:
stack.insert(0,a.left)
#广度优先改stack为deque
最大、最小深度,最大宽度
def maxdepth(root):
depth=0
if root:
left=maxdepth(root.left)
right=maxdepth(root.right)
depth=max(left,right)+1
return depth
def depth(root):
if not root:
return 0
a=[root]
d=0
while a:
b=[]
for node in a:
if a.left:
b.append(a.left)
if a.right:
b.append(a.right)
a=b
d+=1
return d
def mindepth(root):
if not root:
return 0
left=mindepth(root.left)
right=mindepth(root.right)
if left==0 or right==0:
return left+right+1
return min(left,right)+1
#每一层非空节点数的话在最大深度代码中,求b的长度即可
#此代码最大宽度定义为一层中两个非空节点间的节点个数,包括None,LeetCode 662,
def width(root):
queue=[(root,0,0)]
curdepth=ans=left=0
for node.depth,pos in queue:
if node:
queue.append((node.left,depth+1,pos*2))
queue.append((node.right,depth+1,pos*2))
if curdepth!=depth:
left=pos
curdepth=depth
ans=max(ans,pos-left+1)
return ans
最低公共祖先
def lowest(root,a,b):
if not root or root==a or root==b:
reutrn root
left=lowest(root.left,a,b)
right=lowest(root.right,a,b)
if left and right:
return root
if not left:
return right
if not right:
return left
return None
树的所有路径(根到叶节点),树的所有路径中(根到节点)等于某个值,依然借助stack结构,在bfs的基础上增加一个数组记录路径
#递归
def path(root):
if not root:
return []
if not root.left and not root.right:
return [str(root.val)]
return [str(root.val)+'-'+i for i in path(root.left)]+[str(root.val)+'-'+j for j in path(root.right)]
#非递归
def path(root):
if not root:
retrun []
res,stack=[],[(root,'')]
while stack:
node,ls=stack.pop()
if not node.left and not node.right:
res.append(ls+str(node.val))
if node.left:
stack.append((node.left,ls+str(node.left)+'-')
if node.right:
stack.append((node.right,ls+str(node.right)+'-')
return res
def findpath(root,num):
a=[root]
result=[[root.val]]
r=[]
while a:
temp=a.pop()
i=result.pop()
if temp.left:
a.append(temp.left)
result.append(i+[temp.left])
if temp.right:
a.append(temp.right)
result.append(i+[temp.right]
if not temp.left and not temp.right and sum(i)==num:
r.append(i)
return r
字典树
class trienode:
def __init__(self):
self.data={}
def insert(root,word):
for i in word:
child=root.data.get(i)
if not child:
root.data[child]=trienode()
root=root.data[child]
哈夫曼树
import heapq
def make_hufftree(inlist):
trees=list(inlist)
heapq.heapify(trees)
while len(trees)>1:
rightChild,leftChild=heapq.heappop(trees),heapq.heappop(trees)
parentNode=(leftChild[0]+rightChild[0],leftChild,rightChild)
heapq.heappush(trees,parentNode)
class Node(object):
def __init__(self,i):
self.weight=i
self.left=None
self.right=None
def sort(l):
return l.sort(key=lambda node:node.weight)
def hufftree(l):
sort(l)
while len(l)>1:
a=l.pop(0)
b=l.pop(0)
c=Node(a.weight+b.weight)
c.left=a
c.right=b
l.append(c)
sort(l)
return l
判断两棵树是否相同
class Solution:
def isSameTree(self, p, q):
if not p and not q:
return True
if not q or not p:
return False
if p and q:
if p.val!=q.val:
return False
else:
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)