首页 > 其他分享 >day21

day21

时间:2023-02-05 19:23:02浏览次数:39  
标签:right TreeNode val day21 return root left

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


class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root.val > p.val && root.val > q.val){
            return lowestCommonAncestor(root.left, p, q);
        } else if (root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        } else {
            return root;
        }
    }
}

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


class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode addNode = new TreeNode(val);

        if(root == null){
            root = addNode;
        }

        if(root.val > val){
            root.left = insertIntoBST(root.left, val);
        }
        if(root.val < val){
            root.right = insertIntoBST(root.right, val);
        }
        return root;
    }   
}

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


class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null ) {
            return root;
        }

        if(root.val == key) {
            if(root.left == null){
                return root.right;
            } else if(root.right == null){
                return root.left;
            }else {
                TreeNode cur = root.right;
                while(cur.left != null){
                    cur = cur.left;
                } 
                cur.left = root.left;
                root = root.right;
                return root;
            }
        }

        if(root.val > key ) {
            root.left = deleteNode(root.left, key);
        }
        if(root.val < key) {
            root.right = deleteNode(root.right, key);
        }

        return root;
    }
}

标签:right,TreeNode,val,day21,return,root,left
From: https://www.cnblogs.com/hzj-bolg/p/17093814.html

相关文章

  • 剑指offer——Day21 位运算(简单)
    Day212023.2.3位运算(简单)剑指offer15.二进制中1的个数自己实现这个题最简单的做法很容易理解,就是执行while(n!=0)循环,然后在循环中n>>=1,并判断如果n&1==1那么就要对......
  • day21-多线程
    1.实现多线程1.1简单了解多线程【理解】是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多个线程,提升性能......
  • 代码随想录day21 LeetCode 530. 二叉搜索树的最小绝对差 501. 二叉搜索树中的众数 236
    530.二叉搜索树的最小绝对差遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。classSolution{public:vector<int......
  • 字符串函数(day21)
    strcpy函数char*strcpy(char*dst,constchar*src){if((dst==NULL)||(src==NULL))returnNULL;//如果有空指针存在,返回空指针char*ret=dst;//【1】设......
  • 代码随想录算法训练营Day21|530. 二叉搜索树的最小绝对差、501. 二叉搜索树中的众数、
    代码随想录算法训练营Day21|530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236.二叉树的最近公共祖先530.二叉搜索树的最小绝对差530.二叉搜索树的最小绝对......
  • 【算法训练营day21】LeetCode530.二叉搜索树的最小绝对差 LeetCode501. 二叉搜索树中
    LeetCode530.二叉搜索树的最小绝对差题目链接:530.二叉搜索树的最小绝对差初次尝试利用二叉搜索树的性质:中序遍历的结果是有序递增数组,最后遍历该数组得到最小绝对差。c......
  • Day21:方法重写以及注意细节
    目录方法重写什么是方法重写?方法重写有什么用?方法重写的注意细节方法重写什么是方法重写?方法重写指的是当子类和父类出现了一摸一样的方法声明方法重写有什么用?当父类......
  • day21-web开发会话技术03
    WEB开发会话技术0310.问题引出问题引出不同的用户登录网站后,不管该用户浏览网站的哪个页面,都可以显示登录人的名字,还可以随时去查看自己购物车中的商品,这是如何实现......
  • LeetCode刷题记录.Day21
    重复的子字符串459.重复的子字符串-力扣(LeetCode)classSolution{public:voidgetNext(int*next,conststring&s){next[0]=-1;intj......
  • day21
    【面试题02.07.链表相交】/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(......