目录
235. 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
解题思路:
本题合理利用二叉搜索树特征,左小右大进行定位。遍历root,找到p,q大致方向进行遍历。
确认输入参数:def dfs(cur)
停止条件:if not root: return None
递归逻辑:if cur.val>p and cur.val>q, left = dfs(cur.left), if left != None: return left(注意此处返回left因为在左边找到了满足条件的root,逐步返回到最终结果); elif cur.val<p and cur.val<q: right = dfs(cur.right), if right != None: return right
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = 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:
#on the left side of BST
left = self.lowestCommonAncestor(root.left, p, q)
if left != None:
return left#found correct node on this side
elif root.val<p.val and root.val<q.val:
right = self.lowestCommonAncestor(root.right, p, q)
if right != None:
return right
else:
#found the lowestcommon ancestor
return root
701.二叉搜索树中的插入操作
给定二叉搜索树(BST)的根节点 root
和要插入树中的值 value
,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
解题思路:
注意插入的所有node均可在叶子节点上插入,不需要改变BST的整体构造。
输入参数:def insert(cur, value)
停止条件:当遇上空节点时说明在此处插入,返回新建node
递归逻辑:当value>cur.val, cur.right = insert(cur.right, value); value<cur.val, cur.left = insert(cur.left, value)
# 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 insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
#insert node here
new_node = TreeNode(val)
return new_node
if root.val>val:
root.left = self.insertIntoBST(root.left, val)
elif root.val<val:
#go to the right side
root.right = self.insertIntoBST(root.right, val)
return root
450.删除二叉搜索树中的节点
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
- 首先找到需要删除的节点;
- 如果找到了,删除它。
解题思路:
删除节点分为五种情况
- 没有找到要删除的节点
- 要删除的是叶子节点(直接return None)
- 找到要删除的节点,该节点左边为空,右边不为空(root直接连接右边)
- 找到要删除的节点,该节点右边为空,左边不为空(root直接连接左边)
- 该节点左右均不空(连接右边最小的node与左边整个连接)
# 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 deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: #no value root found if not root: return None if root.val == key: #leaf node deleted if not root.left and not root.right: return None elif root.left and not root.right: return root.left elif not root.left and root.right: return root.right elif root.left and root.right: cur = root.right# starts with root.right, find the left most node on the right side while cur.left: cur = cur.left cur.left = root.left#connect root left with cur.left return root.right#return the right side root.left = self.deleteNode(root.left, key) root.right = self.deleteNode(root.right, key) return root