首页 > 其他分享 >前两题写的又快又准 , 第三题写的又慢又乱

前两题写的又快又准 , 第三题写的又慢又乱

时间:2023-01-03 01:22:06浏览次数:22  
标签:right TreeNode val 第三 题写 return null root 快又准

235. 二叉搜索树的最近公共祖先

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if ((root.val >= p.val && root.val <= q.val) || (root.val <= p.val && root.val >= q.val)) {
            return root;
        }
        if (root.val > p.val) {
            return lowestCommonAncestor(root.left, p, q);
        }
        return lowestCommonAncestor(root.right, p, q);
    }

701.二叉搜索树中的插入操作

public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) {
            return new TreeNode(val);
        }
        if (val < root.val) {
            root.left = insertIntoBST(root.left, val);
        } else {
            root.right = insertIntoBST(root.right, val);
        }
        return root;
    }

    public TreeNode insertIntoBST2(TreeNode root, int val) {
        if (root == null) {
            return new TreeNode(val);
        }
        TreeNode cur = root, pre = null;
        while (cur != null) {
            if (cur.val < val) {
                pre = cur;
                cur = cur.right;
            } else {
                pre = cur;
                cur = cur.left;
            }
        }
        if (pre.val < val) {
            pre.right = new TreeNode(val);
        } else {
            pre.left = new TreeNode(val);
        }
        return root;

    }

450.删除二叉搜索树中的节点

public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) {
            return null;
        }
        if(root.val > key){
            root.left=deleteNode(root.left,key);
        } else if (root.val < key) {
            root.right=deleteNode(root.right,key);
        }else {
            if(root.left == null) return root.right;
            if(root.right == null) return root.left;
            TreeNode right = root.right;
            while(right.left != null){
                right = right.left;
            }
            root.val = right.val;
            root.right=deleteNode(root.right,right.val);
        }
        return root;
    }

标签:right,TreeNode,val,第三,题写,return,null,root,快又准
From: https://www.cnblogs.com/Chain-Tian/p/17020943.html

相关文章

  • 周志华《机器学习》课后习题(第三章):线性模型
    作者| 我是韩小琦3.1试分析在什么情况下,在以下式子中不比考虑偏置项b。答:在样本  中有某一个属性  为固定值时。那么此时  等价于偏置项,此时  与  等价。3.2......
  • C# 有什么实用的第三方库吗?
    本文旨在收录一些小众冷门的开源库,像AutoMapper、Autofac、epplus、Hangfire之类的大众都知晓的库,这里就不再推荐了。1.Masuit.Tools首先推荐的的当然是我自家的开源库......
  • Windows批处理实现邮件远程控制电脑(第三方批处理)
    最近网上看到了电子邮箱的新利用方法如题,下载了几个此类软件,发现好几个不是不好用,就是功能不全。上博客园搜了一下,那么可以看到有使用java和python实现的,这里我们用Window......
  • 第三章:路由层
    路由层路由匹配语法#路由层就是项目文件夹下的urls.py文件#urlpatterns就是路由匹配路径urlpatterns=[url(r'^admin/',admin.site.urls),url(r'^home/......
  • Elastic 认证考试团购报名第三季
    0、Elastic认证考试介绍认证通过后,授予的徽章下方会有如上一段话:Elastic认证工程师拥有构建完整的Elasticsearch解决方案的技能。包含:包括部署、配置和管理Elasticsearch集......
  • C++ Primer第三章知识点(想起来啥记啥版)
    命名空间#include<iostream>//using声明,当我们使用名字cin时,从命名空间std中获取它usingstd::cin;intmain(){inti;cin>>i;//正确:cin和st......
  • web项目开发---第三天
    ssm核心业务crm项目的简介:CustomerrelationshipManagement客户关系管理系统销售或者贸易型公司使用。企业级应用,内部员工使用。java开发的传统软件。CRM项目的宗旨......
  • 《程序是怎样跑起来的》第三章
    《程序是怎么跑起来的第三章》:计算机进行小数处理也有可能会出现问题,是在将二进制表示的小数转换为十进制时出现的错误,因为有些十进制的小数无法转换为二进制,也就无法用二......
  • 代码随想录第三天 || 203.移除链表元素 || 707.设计链表 || 206.反转链表
    203.移除链表元素文章:代码随想录(programmercarl.com)视频:https://www.bilibili.com/video/BV18B4y1s7R9classSolution{public:ListNode*removeElements(Lis......
  • 代码随想录算法训练营第三天LeetCode203,707,206
    代码随想录算法训练营第三天|LeetCode203,707,206LeetCode203移除链表元素题目链接:https://leetcode.cn/problems/remove-linked-list-elements/description///区分了删......