669.修剪二叉搜索树
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root:return
# 若root.val > high 则右子树的节点要被剪枝 递归返回trimBST 从root.left开始
if root.val > high:
return self.trimBST(root.left,low,high)
# 若root.val <low 则左子树的节点要被剪枝 递归返回trimBST 从root.right开始
if root.val < low:
return self.trimBST(root.right,low,high)
# 若root.val符合范围,则遍历左右子树
root.left = self.trimBST(root.left,low,high)
root.right = self.trimBST(root.right,low,high)
# 最终执行root
return root
这部分思路我认为比较关键
首先寻找修建后的二叉搜索树的头结点root
while root and (root.val < low or root.val > high):
if root.val > high:
root = root.left
else:
root = root.right
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root:return
# 首先寻找修建后的二叉搜索树的头结点root
while root and (root.val < low or root.val > high):
if root.val > high:
root = root.left
else:
root = root.right
# 修剪root的左子树
cur = root
while cur:
# 当左子树存在且左子树的值小于low
while cur.left and cur.left.val < low:
cur.left = cur.left.right
cur = cur.left
# 修剪root的右子树
cur = root
while cur:
# 当右子树存在且右子树的值大于high
while cur.right and cur.right.val > high:
cur.right = cur.right.left
cur = cur.right
return root
108.将有序数组转换为二叉搜索树
错误代码,当输入示例为nums = [0,1,2,3,4,5]时,输出[3,2,4,1,null,null,5,0],正确答案应为[3,1,5,0,2,4]
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
# 找数组中位置居中的元素作为根节点
n = len(nums)
root_index = n // 2
root = TreeNode(nums[root_index])
cur = root
# 构造左子树
# return nums[:root_index][::-1]
for i in nums[:root_index][::-1]:
cur.left = TreeNode(i)
cur = cur.left
cur = root
for j in nums[root_index+1:]:
cur.right = TreeNode(j)
cur = cur.right
return root
注意要考虑数组为空的情况!!
lass Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
if not nums:return None
n = len(nums)
root_index = n // 2
root = TreeNode(nums[root_index])
root.left = self.sortedArrayToBST(nums[:root_index])
root.right = self.sortedArrayToBST(nums[root_index+1:])
return root
时间复杂度O(nlogn) 空间复杂度O(logn)
538.把二叉搜索树换为累加数
按照“右根左”的顺序,递归遍历二叉搜索树,累加遍历到的所有节点值到 s中,然后每次赋值给对应的 节点
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
# 递归
def dfs(root):
nonlocal s
if not root:return
dfs(root.right)
s += root.val
root.val = s
dfs(root.left)
s = 0
dfs(root)
return root
定义s表示二叉搜索树节点值累加和,遍历二叉树结点。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
s = 0
node = root
while root:
# 若当前节点root右子树为空
if not root.right:
# 将当前节点值添加到s中,更新当前节点值为s
s +=root.val
root.val = s
# 将当前节点更新为root.left
root = root.left
# 若当前节点右子树不为空
else:
# 找到右子树的最左节点(这个节点就是root节点在中序遍历下的后继节点
next = root.right
while next.left and next.left != root:
next = next.left
# 若后继节点next的左子树为空,则将后继节点的左子树指向当前节点并将当前节点更新为root.right
if not next.left:
next.left = root
root = root.right
# 若不为空,则将当前节点的值添加到s中,更新节点值为s,然后将后继节点左子树只想空,并将当前节点更新为root.left
else:
s += root.val
root.val = s
next.left = None
root = root.left
return node