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. RHEL7或CentOS7安装11.2.0.4 RAC碰到的问题

    RHEL7或CentOS7安装11.2.0.4 RAC碰到的问题 随着Linux 版本的普及,但Oracle数据库主流版本仍是11gR2, 的支持不很完美,在Linux 上安装会遇到几处问题,以此记录 ...

  2. Distributed traceability with Spring Cloud: Sleuth and Zipkin

    I. Sleuth 0. Concept Trace A set of spans that form a call tree structure, forms the trace of the re ...

  3. CS229 6.13 Neurons Networks Implements of stack autoencoder

    对于加深网络层数带来的问题,(gradient diffuse  局部最优等)可以使用逐层预训练(pre-training)的方法来避免 Stack-Autoencoder是一种逐层贪婪(Greedy ...

  4. .NET/C#发起GET和POST请求的几种方法

    using System.Net; GET:   1 2 3 var request = (HttpWebRequest)WebRequest.Create("http://www.lead ...

  5. HTMLTestRunner不生成报告

    使用HTMLTestRunner想生成测试报告,尝试了很多次了,就是无法生成,在百度搜索发现是快捷键问题 工具:Pycharm Ctrl+Shift+F10运行不会生成脚本 Alt+Shift+F10 ...

  6. 零基础学习python_文件(28-30课)

    本人小白一枚,随着现在对测试要求越来越高,动不动就要去会一门编程语言,没办法只能学习学习Python,今天看到几个月前还是菜鸟的人突然就已经能使用Python写简单系统了,没办法,虽然之前也简单学习过 ...

  7. 【转】oracle定制定时执行任务

    本节摘要:本节介绍使用oracle自带的job来实现oracle定制定时执行任务. 1.引言 定制定时执行的任务有两种形式,系统级别和数据库级别, 从操作系统级别来讲, windows系统我们可以使用 ...

  8. win7 安装英文语言包

    因为某些英文程序字符显示不全,所以考虑把 win7 改为英文语言.直接下载英文语言包安装不成功,经过多次尝试和百度终于找到合适的办法. 下载 Vistalizator.exe, windows6.1- ...

  9. My sql 自增 虚拟列。

    在MYSQL 是没有类似MSSQL 2008 / oracle 数据库开窗函数 over() ,rank(), DENSE_RANK() ,ROW_NUMBER() 又叫窗口函数 . 当我们需要在查询 ...

  10. uva-310-L--system-暴力枚举

    题意:输入四个字符串a,b,w,z,经过一定的替换规则,问w或者w的子串中是否包含z. 替换规则如下.w中的字符a全部替换成a字符串,b字符全部替换成b字符串. 枚举过程, 根据替换规则对w进行替换, ...