首页 > 其他分享 >450. 删除二叉搜索树中的节点(leetcode)

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

时间:2024-05-11 12:42:10浏览次数:15  
标签:right 删除 450 树中 节点 null root leetcode left

https://leetcode.cn/problems/delete-node-in-a-bst/

要点是确定函数语义,单层递归逻辑,确定好有返回值的这种写法,分析出5种情况,递归的时候接收好递归的返回的新树的根节点即可

class Solution {

    // 函数语义(单层递归逻辑):查找要删除的节点,并且返回删除节点后新的树的根节点
    public TreeNode deleteNode(TreeNode root, int key) {
        // 删除节点
        // 第一种情况:没找到删除的节点,遍历到空节点直接返回了
        if(root==null)return null;
        if(root.val==key)
        {
            // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
            if(root.left==root.right && root.left==null)return null;
            // 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
            else if(root.left==null && root.right!=null)return root.right;
            // 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
            else if(root.right==null && root.left!=null)return root.left;
            // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。    
            else
            {
                TreeNode cur=root.right;
                while(cur.left!=null)cur=cur.left;
                cur.left=root.left;
                return root.right;
            }
        }
        // 不删除节点
        // 实现函数语义
        if(root.val > key)root.left=deleteNode(root.left,key);
        else root.right=deleteNode(root.right,key);
        return root;
    }
}

 

标签:right,删除,450,树中,节点,null,root,leetcode,left
From: https://www.cnblogs.com/lxl-233/p/18186268

相关文章

  • [LeetCode] 最短的桥 双BFS Java
    Problem:934.最短的桥目录思路复杂度Code思路先找到第一个岛屿,根据每一个岛屿的岛屿块的位置多源查找这个块与第二个岛屿的距离,先找到的就是最少的距离同时,将已遍历过的岛屿标记为-1,避免重复入队复杂度时间复杂度:添加时间复杂度,示例:$O(n^2)$空间复杂度:添......
  • LeetCode 1186. Maximum Subarray Sum with One Deletion
    原题链接在这里:https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/题目:Givenanarrayofintegers,returnthemaximumsumfora non-empty subarray(contiguouselements)withatmostoneelementdeletion. Inotherwords,youwa......
  • LeetCode 2210. Count Hills and Valleys in an Array
    原题链接在这里:https://leetcode.com/problems/count-hills-and-valleys-in-an-array/description/题目:Youaregivena 0-indexed integerarray nums.Anindex i ispartofa hill in nums iftheclosestnon-equalneighborsof i aresmallerthan nums[i].......
  • 235. 二叉搜索树的最近公共祖先(leetcode)
    https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/要点是如果root是在p和q之间的值,意味着已经找到了最近公共祖先/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*rig......
  • 236. 二叉树的最近公共祖先(leetcode)
    https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/要点是后序遍历,这样就可以从树底开始搜索,寻找最近公共祖先classSolution{public://返回的是最近公共祖先,若返回null则说明没有TreeNode*lowestCommonAncestor(TreeNode*r......
  • leetCode 001.两数之和
    给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。示例1:输入:nums=[2,7,11,15],ta......
  • LeetCode 049. 字母异位词分组
    给你一个字符串数组,请你将字母异位词组合在一起。可以按任意顺序返回结果列表。字母异位词是由重新排列源单词的所有字母得到的一个新单词。示例1:输入:strs=["eat","tea","tan","ate","nat","bat"]输出:[["bat"],["nat","tan"],["......
  • leetCode 128. 最长连续序列
    给定一个未排序的整数数组nums,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为O(n)的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。示例2:输入:nums=[0,3,7,......
  • Leetcode --- 203. 移除链表元素
    题目描述给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val==val 的节点,并返回 新的头节点 。示例1: 示例输入:head=[1,2,6,3,4,5,6],val=6输出:[1,2,3,4,5]输入:head=[7,7,7,7],val=7输出:[]参考实现方式1、使用递归实现......
  • [LeetCode] 2487. Remove Nodes From Linked List
    Youaregiventheheadofalinkedlist.Removeeverynodewhichhasanodewithagreatervalueanywheretotherightsideofit.Returntheheadofthemodifiedlinkedlist.Example1:Input:head=[5,2,13,3,8]Output:[13,8]Explanation:Thenodesth......