title: LEETCODE-DAY22
date: 2024-03-13 16:14:28
tags:
今日内容:235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点
T1
这题比较有意思,可以证明从根节点开始遍历第一个落入区间p,q的节点A即为p,q的最近公共祖先
证明1.显然节点A为p,q公共祖先,因为取该二叉搜索树的中序遍历,节点A位于p,q之间,由中序遍历的特性p,q分别在以A为根节点的子树两侧。
2.A为最近公共祖先。反证假设有节点B也为p,q公共祖先且深度更大,则B的值一定落在[p,A]或[A,q]中。若B落在[p,A]中则B不可能成为q的祖先节点,矛盾。
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root:
return root
q=list()
q.append(root)
while q:
temp=q.pop()
if temp.val <= max(p.val,q.val) and temp.val>=min(p.val,q.val):
return temp
if temp.right:
q.append(temp.right)
if temp.left:
q.append(temp.left)
AttributeError: ‘list’ object has no attribute ‘val’
^^^^^
if temp.val <= max(p.val,q.val) and temp.val>=min(p.val,q.val):
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
cur=root
while cur:
if cur.val >max(p.val,q.val):
cur=cur.right
elif cur.val < min(p.val,q.val):
cur=cur.left
else:
return cur
输入
[6,2,8,0,4,7,9,null,null,3,5]
2
4
输出
null
预期结果
2
未知BUG(逻辑反了)
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
cur=root
while cur:
if cur.val >max(p.val,q.val):
cur=cur.left
elif cur.val < min(p.val,q.val):
cur=cur.right
else:
return cur
T2
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
root=TreeNode(val)
cur=root
while cur:
if cur.val > val:
cur=cur.left
elif cur.val < val:
cur=cur.right
cur=TreeNode(val)
return cur
错误
输入
root =
[4,2,7,1,3]
val =
5
输出
[5]
预期结果
[4,2,7,1,3,5]
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
root=TreeNode(val)
return root
cur=root
while cur:
if cur.val > val:
cur=cur.left
elif cur.val < val:
cur=cur.right
cur=TreeNode(val)
return root
root =
[4,2,7,1,3]
val =
5
输出
[4,2,7,1,3]
预期结果
[4,2,7,1,3,5]
原本想的是cur跳出循环时指向树中空节点,把val插入此处即可
修改:还需要记录插入位置,即父节点
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
root=TreeNode(val)
cur=root
while cur:
parent=cur
if cur.val > val:
cur=cur.left
elif cur.val < val:
cur=cur.right
if not parent.left:
parent.left=TreeNode(val)
elif not parent.right:
parent.right=TreeNode(val)
return root
输入
root =
[61,46,66,43,null,null,null,39,null,null,null]
val =
88
输出
[61,46,66,43,null,88,null,39]
预期结果
[61,46,66,43,null,null,88,39]
class Solution:
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
root=TreeNode(val)
return root
cur=root
while cur:
parent=cur
if cur.val > val:
cur=cur.left
elif cur.val < val:
cur=cur.right
if not parent.left and parent.val>val:
parent.left=TreeNode(val)
elif not parent.right and parent.val <val:
parent.right=TreeNode(val)
return root
AC
T3
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 and not root.right:
return None
elif not root.left:
return root.right
elif not root.right:
return root.left
else:
cur=root.right
while cur.left:
cur=cur.left
cur.left=root.left
return root.right
if key <root.val:
root=root.left
if key > root.val:
root=root.right
root =
[5,3,6,2,4,null,7]
key =
3
输出
[]
预期结果
[5,4,6,2,null,null,7]
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 and not root.right:
return None
elif not root.left:
return root.right
elif not root.right:
return root.left
else:
cur=root.right
while cur.left:
cur=cur.left
cur.left=root.left
return root.right
if key < root.val:
return self.deleteNode(root.left,key)
if key > root.val:
return self.deleteNode(root.right,key)
输入
root =
[5,3,6,2,4,null,7]
key =
3
输出
[4,2]
预期结果
[5,4,6,2,null,null,7]
if key < root.val:
root.left=self.deleteNode(root.left,key)
if key > root.val:
root.right=self.deleteNode(root.right,key)
输入
root =
[5,3,6,2,4,null,7]
key =
3
输出
[]
预期结果
[5,4,6,2,null,null,7]
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 and not root.right:
return None
elif not root.left:
return root.right
elif not root.right:
return root.left
else:
cur=root.right
while cur.left:
cur=cur.left
cur.left=root.left
return root.right
if key < root.val:
root.left=self.deleteNode(root.left,key)
if key > root.val:
root.right=self.deleteNode(root.right,key)
return root
AC
递归具体写法还要再加强
文章讲解了如何在二叉搜索树中执行最近公共祖先查找、节点插入和删除,涉及递归方法和实例代码。
338

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



