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

Subscribe to see which companies asked this question

解答:

啊哈这个操作好常规啊……假设这个函数可以在以root为根节点的子树中删除值为key的节点并返回新的BST的根节点,先通过BST的性质递归找到待删除节点,如果是叶节点就直接删,如果左右子节点有NULL就直接删了把下面的子树接上去,如果是左右子节点均存在就把中缀后继的值换上来然后在右子树中继续调用函数删除中缀后继的那个节点,最后递归返回。注意递归延查找路径返回时,一定要先将递归调用的返回值赋值给上一层调用中的树的节点,而不是单纯的返回递归调用的返回值,即应该是:

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

而不是

else if(key < root->val){
        return deleteNode(root->left, key);
    }
    else{
        return deleteNode(root->right, key);
    }

因为如果按第二种那样返回的话最后得到的根节点有可能只是原来整棵树的一部分……

当然啦,作为树的题目,一如既往的应该考虑root为NULL的情况……

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* deleteNode(struct TreeNode* root, int key) {
    struct TreeNode *tmp;

    if(NULL == root)
        return NULL;

    if(root->val == key){
        if(NULL == root->left&&NULL == root->right){
            return NULL;
        }
        else if(NULL == root->left){
            return root->right;
        }
        else if(NULL == root->right){
            return root->left;
        }
        else{
            tmp = root->right;
            while(NULL != tmp->left){
                tmp = tmp->left;
            }
            root->val = tmp->val;
            root->right = deleteNode(root->right, tmp->val);
        }
    }
    else if(key < root->val){
        root->left = deleteNode(root->left, key);
    }
    else{
        root->right = deleteNode(root->right, key);
    }
    return root;
}

LeetCode OJ 450. Delete Node in a BST的更多相关文章

  1. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

  2. 【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. Retur ...

  3. [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. Retur ...

  4. [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. Retur ...

  5. [leetcode]450. Delete Node in a BST二叉搜索树删除节点

    二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...

  6. 450. Delete Node in a BST 删除bst中的一个节点

    [抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. ...

  7. 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. Retur ...

  8. LC 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. Retur ...

  9. LeetCode OJ:Delete Node in a Linked List(链表节点删除)

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

随机推荐

  1. Abp.AutoMapper扩展(1) --static class AutoMapExtensions

    // 摘要:        //     Converts an object to another using AutoMapper library. Creates a new object    ...

  2. U3D学习003——编辑器使用

    1.skybox 原来的render setting 在2017版本中是lighting标签environment中设置: 或者在摄像机对象中添加skybox组件,进行设置. 2.6张图实现自定义sk ...

  3. 2018 oppo校招前端面试题

    1.Es6 2.http请求过程 3.js事件执行流程(蒙蔽中) [默认冒泡,由内到外,] 4.css 样式选择器的优先级 (!important在类选择器和id选择器都可以使用,但不推荐使用) 5. ...

  4. spring启动找不到spring.liveBeansView.mbeanDomain配置

    做项目时,启动tomcat报错 JNDI lookup for name [spring.liveBeansView.mbeanDomain] threw NamingException with m ...

  5. java 模拟浏览器发送post请求

    java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...

  6. CSS3帧动画

    在前面的文章中也有介绍过css3动画的内容,可见<关于transition和animation>和<webkitAnimationEnd动画事件>,今天又要唠叨一下这个东西了, ...

  7. 零基础学习python_爬虫(53课)

    1.Url的格式简单介绍,如下图: 2.我们要对网站进行访问,需要用到python中的一个模块或者说一个包吧,urllib(这个在python2中是urllib+urllib2,python3将这两个 ...

  8. django---单表操作之增删改

    首先找到操作的首页面‘ 代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  9. lampp中的ftp使用介绍

    搭建完毕lampp 具体要求如下:使用Lampp的proftpd,开通多个FTP用户,并各分配一个目录,而且需要限制用户在自己的目录里面,可以自由读写. 操作步骤:第一步:设置ftp用户组,输入命令: ...

  10. Maven项目的生命周期

    Maven中存在三套生命周期,每一套生命周期相互独立,互不影响.在一套生命周期内,执行后面的命令前面的命令会自动执行. CleanLifeCycle:清理生命周期 mvn clean DefaultL ...