LeetCode OJ 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:
- 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 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的更多相关文章
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 【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 ...
- [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 ...
- [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 ...
- [leetcode]450. Delete Node in a BST二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 第32课 Linux内核链表剖析
1. Linux内核链表的位置及依赖 (1)位置:{linux-2.6.39}\\include\linux\list.h (2)依赖 ①#include<linux\types.h> ② ...
- 数组.html
<script > var arr1 = [1, 2, 3, 4, 5, 6 ]; 赋值 var arr2 =Array(1,2,3,4,5,6); var arr3 = new Arra ...
- 入坑机器学习?听听MIT在读博士的AI心得
随着人工智能技术的火热,越来越多的年轻学者正准备投身其中,开启自己的研究之路.和所有其他学科一样,人工智能领域的新人总会遇到各种各样的难题,其中不仅有研究上的,也有生活方面的.MIT EECS 在读博 ...
- TestLink 的使用详解
二.初始配置(设置用户.产品) 1. 用户设置 在TestLink系统中,每个用户都可以维护自己的私有信息.admin可以创建用户,但不能看到其它用户的密码.在用户信息中,需要设置Email地址,如果 ...
- qt tcp 通信实例
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QHostAddress> ...
- Fiddler设置抓取https请求
环境准备 1.安装最新版本的Fiddler程序 官网地址:https://www.telerik.com/fiddler 本文写的时候,fiddler最新的版本为5.0 2.安装fiddler证书生成 ...
- Java中同步的几种实现方式
1.使用synchronized关键字修饰类或者代码块: 2.使用Volatile关键字修饰变量: 3.在类中加入重入锁. 代码示例: 非同步状态下: public static void main( ...
- 插入排序(直接插入、折半、Shell)
直接插入排序(顺序插入排序) 基本思想: 排序过程,整个排序过程为n-1趟插入,即先将序列中的第1个元素看成是一个有序子序列,然后从第2个元素开始,逐个进行插入,直至整个序列有序. 在有序序列中插入一 ...
- 在虚拟机中安装ubuntu
初始安装: 1.安装新虚拟机时,选择稍后安装操作系统,这可以自己设置语言等信息 2.修改自定义硬件:为网卡生成一个mac地址,(这里需要注意,有时网卡会冲突,导致连接时好时坏,以后可以删除掉网卡,重新 ...
- springBoot基本配置
Spring Boot 基本配置 1.新建maven jar工程 使用依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" ...