主要内容
- 二叉搜索树最近公共祖先
- 二叉搜索树插入
- 二叉搜索树删除
题目
235 二叉搜索树的最近公共祖先
思路分析
因为是有序的,从上到下遍历,只要cur.val 在[p.val, q.val] 之间,cur就是要返回的值
代码
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 按普通二叉树的思路来呢
if not root:
return None
if root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right:
return root
elif not left and right:
return right
elif left and not right:
return left
else:
return None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 递归,二叉搜索树
if not root:
return None
if root.val > p.val and root.val > q.val:
left = self.lowestCommonAncestor(root.left, p, q)
if left:
return left
if root.val < p.val and root.val < q.val:
right = self.lowestCommonAncestor(root.right, p, q)
if right:
return right
return root
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 迭代,二叉搜索树
while root:
if root.val > p.val and root.val > q.val:
root = root.left
elif root.val < p.val and root.val < q.val:
root = root.right
else:
return root
return None
701 二叉搜索树中的插入操作
思路分析
若节点不为空,返回的是当前节点,由上一层接住
代码
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if root is None:
return TreeNode(val)
if root.val > val:
root.left = self.insertIntoBST(root.left, val)
if root.val < val:
root.right = self.insertIntoBST(root.right, val)
return root
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
node = TreeNode(val)
if root is None:
return node
if root.left is None and root.val > val:
root.left = node
if root.right is None and root.val < val:
root.right = node
if root.val > val:
self.insertIntoBST(root.left, val)
if root.val < val:
self.insertIntoBST(root.right, val)
return root
450 删除二叉搜索树中的节点
思路分析
第一遍自己的思路:迭代
递归:
一、遇到空节点,返回
二、cur.val == key:
- left 为空,right补上
- right 为空,left补上
- left,放在right左孩子左叶子节点后面
代码
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
# 迭代
if not root:
return None
if root.val == key:
return self.newTree(root)
cur = root
while cur:
if cur.val > key:
# 向左找
if cur.left and cur.left.val == key:
cur.left = self.newTree(cur.left)
return root
cur = cur.left
elif cur.val < key:
# 向右找
if cur.right and cur.right.val == key:
cur.right = self.newTree(cur.right)
return root
cur = cur.right
return root
def newTree(self, cur):
# 将以cur为根的左右子树,重组为二叉搜索树
if not cur.left and not cur.right:
return None
nums = []
def _traversal(node):
if not node:
return
_traversal(node.left)
nums.append(node.val)
_traversal(node.right)
_traversal(cur)
nums.remove(cur.val)
r = TreeNode(nums[0])
p = r
for i in range(1, len(nums)):
p.right = TreeNode(nums[i])
p = p.right
return r
标准做法
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
# 递归
if not root:
return None
if root.val == key:
# 左孩子为空
if not root.left:
return root.right
# 右孩子为空
elif not root.right:
return root.left
# 均不为空
else:
tmp = root.right
# 找到左叶子
while tmp.left:
tmp = tmp.left
tmp.left = root.left
return root.right
if root.val < key:
root.right = self.deleteNode(root.right, key)
if root.val > key:
root.left = self.deleteNode(root.left, key)
return root