zoukankan      html  css  js  c++  java
  • [LeetCode] 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

    Basically, the deletion can be divided into two stages:

    1. Search for a node to remove.
    2. If the node is found, delete the node.

    Note: Time complexity should be O(height of tree).

    Example:

    root = [5,3,6,2,4,null,7]
    key = 3
    
        5
       / 
      3   6
     /    
    2   4   7
    
    Given key to delete is 3. So we find the node with value 3 and delete it.
    
    One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
    
        5
       / 
      4   6
     /     
    2       7
    
    Another valid answer is [5,2,6,null,4,null,7].
    
        5
       / 
      2   6
          
        4   7

    删除二叉搜索树中的节点。题意是给一个二叉搜索树和一个节点key,请将key从BST中删除并维持BST的性质。

    因为是BST所以查找要删除的节点应该很简单,如果比root小就往左走,比root大就往右走。难点在于删除了节点之后如何维持剩下的节点依然是一个BST。分如下几种情况,

    1. 如果要删除的节点没有左子树,则直接用这个节点的右孩子顶上来;

    2. 如果要删除的节点没有右子树,则直接用这个节点的左孩子顶上来;

    3. 如果要删除的节点有左右子树,需要去右子树里面找出最小的节点顶上来,这个节点是右子树里面最小的左孩子

    对于第一种情况来说:我们要删除节点5(root),直接 return root.right 即可。

    对于第二种情况来说:我们要删除节点5(root),直接 return root.left 即可。

    对于第三种情况来说:我们要删除节点5(root),只需将root的左子树放到root的右子树的最下面的左叶子节点的左子树上即可。如图所示:

    作者:ming-zhi-shan-you--m9RfkvKDad
    链接:https://leetcode-cn.com/problems/delete-node-in-a-bst/solution/450-shan-chu-er-cha-sou-suo-shu-zhong-de-jie-dia-6/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    时间O(h) - 题目要求

    空间O(h) - 题目要求

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public TreeNode deleteNode(TreeNode root, int key) {
    12         // corner case
    13         if (root == null) {
    14             return root;
    15         }
    16 
    17         // normal case
    18         if (key < root.val) {
    19             root.left = deleteNode(root.left, key);
    20         } else if (key > root.val) {
    21             root.right = deleteNode(root.right, key);
    22         } else {
    23             if (root.left == null) {
    24                 return root.right;
    25             } else if (root.right == null) {
    26                 return root.left;
    27             } else {
    28                 TreeNode node = root.right;
    29                 while (node.left != null) {
    30                     node = node.left;
    31                 }
    32                 node.left = root.left;
    33                 return root.right;
    34             }
    35         }
    36         return root;
    37     }
    38 }

    JavaScript实现

     1 /**
     2  * @param {TreeNode} root
     3  * @param {number} key
     4  * @return {TreeNode}
     5  */
     6 var deleteNode = function (root, key) {
     7     // corner case
     8     if (root == null) {
     9         return null;
    10     }
    11 
    12     // normal case
    13     if (key < root.val) {
    14         root.left = deleteNode(root.left, key);
    15     } else if (key > root.val) {
    16         root.right = deleteNode(root.right, key);
    17     } else {
    18         if (root.left == null) {
    19             return root.right;
    20         } else if (root.right == null) {
    21             return root.left;
    22         }
    23         let minNode = findMin(root.right);
    24         root.val = minNode.val;
    25         root.right = deleteNode(root.right, root.val);
    26     }
    27     return root;
    28 };
    29 
    30 var findMin = function (node) {
    31     while (node.left !== null) {
    32         node = node.left;
    33     }
    34     return node;
    35 };

    LeetCode 题目总结

  • 相关阅读:
    Flask 【第三篇】使用DBUtils实现数据库连接池和蓝图
    Flask 【第二篇】Flask基础
    Flask 【第一篇】Flask简介
    Django 【第二十五篇】Django admin源码解析
    Django 【第二十四篇】Django admin简单使用
    Django 【第二十三篇】优化查询
    Django 【第二十二篇】ModelForm
    Django 【第二十一篇】中介模型以及优化查询以及CBV模式
    Django 【第二十篇】后端CORS解决跨域问题
    查询续与ajax
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12466922.html
Copyright © 2011-2022 走看看