LEETCODE-DAY22


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

递归具体写法还要再加强


  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,可以看出这是一段使用迭代器进行循环遍历的代码,并且遍历的对象是`vector<int>`类型的向量。迭代器`it`初始化为`c.begin()`,结束条件为`it < c.end()`。这段代码中省略了循环体,需要根据具体上下文来确定循环体的具体操作。 根据引用,我们可以了解到`vector`是一种动态数组,可以存储多个相同类型的元素。代码示例中用`assign`函数将另一个向量的一部分元素赋值给目标向量。具体来说,`a.assign(b.begin(), b.begin()+3)`意味着将向量`b`的前三个元素赋值给向量`a`。 根据引用,可以看出这是一段关于在VSCode中安装leetcode插件和配置的说明文档。文档中提到了如何安装插件、如何编译和构建代码等内容。 综上所述,LeetCode的`vector`是一种动态数组,可以存储多个相同类型的元素。可以通过迭代器对其进行循环遍历,也可以使用成员函数`assign`来赋值部分元素。在VSCode中,可以安装LeetCode插件并进行相关配置来进行编译和构建。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++LeetCode每日一题 Day3 常用容器vector的使用](https://blog.csdn.net/m0_75276704/article/details/129737396)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vscode安装leetcode-Leetcode:力码](https://download.csdn.net/download/weixin_38557935/20051243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值