Leetcode450 Delete Node in a BST

Problem Definition

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:

Search for a node to remove.
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

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.

在这里插入图片描述

Another valid answer is [5,2,6,null,4,null,7].

在这里插入图片描述

Solutions

solution one: Recursive 1

This is the solution that I come up with by myself. Actually when I considered this method, I have ignored “BST” in the problem title. So that this method is useful for all kinds of trees.
The main idea is that we visit all the nodes in the function deleteNode, and at each node, firstly we check whether the value of the current node equals to key, if it is, we use some rules to change the structure of the tree. And what is the rule that I define? If the left node of target node exists, I replace the target node with its left node. But if it’s left node doesn’t exist, I use it right node to replace the current node. Because the value of the child nodes of the current node also can be target number. So that I use the while loop here to solve this problem. As I mentioned before, the method that I come up with by myself can be used in all kinds of trees. So that maybe multiple nodes have the same value, but in the problem that defined, uh, maybe we just use if but not while loop.

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root == null)    return null;
        while(root != null && root.val == key){
            if(root.left == null && root.right == null) root = null;
            else if(root.left != null){ 
                TreeNode right = root.right;
                TreeNode leftrightmost = root.left;
                while(leftrightmost.right != null){
                    leftrightmost = leftrightmost.right;
                }
                root = root.left;
                leftrightmost.right = right;
            }else if(root.right != null) root = root.right;
        }
        if(root == null)    return null;
        TreeNode node = root;
        node.left = deleteNode(node.left, key);
        node.right = deleteNode(node.right, key);
        return node;
    }
}

Time complexity: O(n)

Solutions two: Recursive 2

In this method, I will use the feature of the binary search tree to solve the problem. As you can see in the binary search, the value of the left note is always less than the current node. And the value of the right note is always bigger than the current node. So that we can use the feature in the process of finding the right note, which is the target node. In the function if the current node is larger than the target node. We should only visit the right child nodes of the current node. But if the current node is larger than the target, we should only visit the left child nodes of the current node. That will improve the efficiency of the recursive method.

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

Time complexity: O(logn)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值