1.669. 修剪二叉搜索树
题目链接:669. 修剪二叉搜索树
文档讲解: 代码随想录
我的思路:这道题和昨天450.删除二叉搜索树中的节点很像,如果要删除节点,还是将删除节点的左子树移动到右子树最左面的节点上。一开始用的是前序遍历。在力扣上是不通过的,我觉得可能是中间节点处理顺序导致的,然后改成后序遍历就可以了,自底向上递归。
class Solution(object):
def trimBST(self, root, low, high):
"""
:type root: TreeNode
:type low: int
:type high: int
:rtype: TreeNode
"""
if not root:
return root
#自底向上,后序遍历
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high)
if root.val < low or root.val > high:
if not root.right:
return root.left
cur = root.right
while cur.left:
cur = cur.left
cur.left = root.left
return root.right
return root
思路:不用重构二叉树,以下图为例,low = 1, high = 3,0 是要删除的节点,只需要将 0 的右孩子直接赋给3的左孩子就可以了。

class Solution(object):
def trimBST(self, root, low, high):
"""
:type root: TreeNode
:type low: int
:type high: int
:rtype: TreeNode
"""
if not root:
return root
if root.val < low:
#在右子树寻找符合区间的节点
return self.trimBST(root.right, low, high)
if root.val > high:
return self.trimBST(root.left, low, high)
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high)
return root
迭代法:
class Solution(object):
def trimBST(self, root, low, high):
"""
:type root: TreeNode
:type low: int
:type high: int
:rtype: TreeNode
"""
if not root:
return root
#寻找合适的头节点
while root and (root.val < low or root.val > high):
if root.val < low:
root = root.right
else:
root = root.left
cur = root
#处理左孩子元素小于low的情况
while cur:
while cur.left and cur.left.val < low:
#删除节点
cur.left = cur.left.right
cur = cur.left
cur = root
while cur:
while cur.right and cur.right.val > high:
cur.right = cur.right.left
cur = cur.right
return root
2.108.将有序数组转换为二叉搜索树
题目链接:108.将有序数组转换为二叉搜索树
文档讲解: 代码随想录
class Solution(object):
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
root = self.traversal(nums, 0, len(nums) - 1)
return root
def traversal(self, nums, left, right):
if left > right:
#定义的是左闭右闭区间,所以此时为空节点
return None
mid = left + (right - left) // 2
root = TreeNode(nums[mid])
root.left = self.traversal(nums, left, mid - 1)
root.right = self.traversal(nums, mid + 1, right)
return root
class Solution(object):
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.sortedArrayToBST(nums[:mid])
root.right = self.sortedArrayToBST(nums[mid + 1:])
return root
迭代法:
class Solution(object):
def sortedArrayToBST(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return None
nodeQue = collections.deque()
leftQue = collections.deque()
rightQue = collections.deque()
root = TreeNode(0)
nodeQue.append(root)
leftQue.append(0)
rightQue.append(len(nums) - 1)
while nodeQue:
cur = nodeQue.popleft()
left = leftQue.popleft()
right = rightQue.popleft()
mid = left + (right - left) // 2
cur.val = nums[mid]
#处理左区间
if left < mid:
cur.left = TreeNode(0)
nodeQue.append(cur.left)
leftQue.append(left)
rightQue.append(mid - 1)
if right > mid:
cur.right = TreeNode(0)
nodeQue.append(cur.right)
leftQue.append(mid + 1)
rightQue.append(right)
return root
3.538.把二叉搜索树转换为累加树
题目链接:538.把二叉搜索树转换为累加树
文档讲解: 代码随想录
思路:二叉搜索树是有序的,从树中可以看出累加顺序为右中左,然后顺序累加。本题需要利用pre指针来记录当前遍历节点cur的前一个节点。
class Solution(object):
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
self.pre = 0
#self.count = 0
self.traversal(root)
return root
def traversal(self, cur):
if not cur:
return
self.traversal(cur.right)
cur.val += self.pre
self.pre = cur.val
# self.count += cur.val
# cur.val = self.count
self.traversal(cur.left)
迭代法:
class Solution(object):
def convertBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root:
return root
stack = []
cur = root
pre = 0
while cur or stack:
if cur:
stack.append(cur)
cur = cur.right
else:
cur = stack.pop()
cur.val += pre
pre = cur.val
cur = cur.left
return root
1544

被折叠的 条评论
为什么被折叠?



