1.二叉搜索树的后序遍历序列
class Solution:
def VerifySquenceOfBST(self , sequence: List[int]) -> bool:
# write code here
# 使用递归的思想。由于后序遍历为左右根,因此序列最后一位一定是根节点
# 此外,二叉搜索树的左子树小于根节点,右子树大于根节点的特点,可根据根节点分别找到左右子树的序列
# 然后递归左右子树看是不是二叉搜索树
if not sequence:
return False
index = 0
for s in sequence:
if s >= sequence[-1]:
index = sequence.index(s)
break
# 如果右子树中有比根节点小的,那么直接判定为False
for i in range(index,len(sequence)):
if sequence[i] < sequence[-1]:
return False
left = True
if index > 0:
left = self.VerifySquenceOfBST(sequence[:index])
right = True
if index < len(sequence) - 1:
right = self.VerifySquenceOfBST(sequence[index:-1])
return left and right
2.二叉树中和为某一值的路径(二)
class Solution:
def FindPath(self , root: TreeNode, target: int) -> List[List[int]]:
# write code here
# 也是使用递归的思想,使用一个全局变量来迭代地装结果
# 当递归在几个步骤内进行时,可以使用另写一个函数来存放递归
# 当游走到叶子结点时,目前的path和已经满足条件,那么加入结果中
# 当不是叶子结点时,把当前节点值加入path中,并递归地游走它的左右子树
self.res = []
self.num = target
if not root:
return self.res
self.DFS(root, [root.val])
return self.res
def DFS(self, root, path):
if not root.left and not root.right and sum(path) == self.num:
self.res.append(path)
if root.left:
self.DFS(root.left, path+[root.left.val])
if root.right:
self.DFS(root.right, path+[root.right.val])
2. 二叉搜索树与双向链表
class Solution:
def __init__(self):
self.cur = None
self.head = None
def Convert(self , pRootOfTree):
# write code here
# 一定要记住中序遍历模板!!!!
# 在中序遍历过程中进行操作,共用到两个指针,cur为当前指向。head为记录最左的指针
if not pRootOfTree:
return None
self.Convert(pRootOfTree.left)
if not self.cur:
self.cur = pRootOfTree
self.head = pRootOfTree
else:
self.cur.right = pRootOfTree
pRootOfTree.left = self.cur
self.cur = pRootOfTree
self.Convert(pRootOfTree.right)
return self.head
# 一定要记住中序遍历模板!!!!
# 先按中序遍历形成list(包裹TreeNode类的list)
# 按顺序把这个list中的所有点分别用两个指针连起来
if not pRootOfTree:
return None
midt = []
def midTr(root):
if not root:
return
midTr(root.left)
midt.append(root)
midTr(root.right)
midTr(pRootOfTree)
for i,v in enumerate(midt[:-1]):
v.right = midt[i+1]
midt[i+1].left = v
return midt[0]
3. 二叉树的下一个结点
class Solution:
def GetNext(self, pNode):
# write code here
# 根据中序遍历的特点,归纳总结出一些规律,可以把情况分两大类讨论
# 如图所示
if not pNode:
return None
if pNode.right:
pNode = pNode.right
while pNode.left:
pNode = pNode.left
return pNode
while pNode.next:
if pNode == pNode.next.left:
return pNode.next
pNode = pNode.next
return None
# 最简单的暴力破解法,直接找到总根节点,然后通过中序遍历得到list,然后再找目标值的后一个位置
num = pNode
while pNode.next:
pNode = pNode.next
midtr = []
def midTr(root):
if not root:
return
midTr(root.left)
midtr.append(root)
midTr(root.right)
midTr(pNode)
if midtr.index(num) == len(midtr) - 1:
return None
return midtr[midtr.index(num)+1]
4.把二叉树打印成多行
class Solution:
def Print(self , pRoot: TreeNode) -> List[List[int]]:
# write code here
# 和按之字形打印二叉树几乎一样,只是之字形更麻烦需要在最后对奇偶数行进行判断并反转
# 套用了遍历二叉树的模板,不一样的是stack不为累加,而是每次都清空变成下一行的左右节点
res = []
if not pRoot:
return res
stack = [pRoot]
while stack:
tmp_res = []
nextstack = []
for n in stack:
tmp_res.append(n.val)
if n.left:
nextstack.append(n.left)
if n.right:
nextstack.append(n.right)
res.append(tmp_res)
stack = nextstack
return res