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
思路:
注意是BST 已经排好序了。
找到要删除的node;
node 不含左右节点 返回null;
node 只含有左子树,返回左子树
node只含有右子树,返回右子树
node 左右子树都有,找到右子树种最下的值,赋给node,递归地删掉 有字数中最小的元素
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root==null) return null ;
if(root.val>key) root.left=deleteNode(root.left,key);
else if(root.val<key) root.right = deleteNode(root.right,key);
else { if(root.left==null) return root.right;
if(root.right==null) return root.left; TreeNode rightmin = findmin(root.right);
root.val = rightmin.val;
root.right = deleteNode(root.right,root.val);
}
return root;
}
private TreeNode findmin(TreeNode root){
while(root.left!=null)
root=root.left;
return root;
} }
450. Delete Node in a BST的更多相关文章
- [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 ...
- 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. ...
- 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. 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】 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 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- [leetcode]450. Delete Node in a BST二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
随机推荐
- IntelliJ Idea Hide excluded folders 隐藏或显示你需要的文件夹
IntelliJ Idea,以前用idea时,经常maven编译就出现了exclude下的文件夹通常是target,如何隐藏自己不想看见的文件夹,或显示偶尔会用到的文件夹 点击齿轮右下小标 选中文件夹 ...
- 0、手把手教React Native实战之开山篇
##作者简介 东方耀 Android开发 RN技术 facebook github android ios 原生开发 react reactjs nodejs 前端 ...
- 组合使用QT的资源管理高级功能简化开发过程
使用 QT 进行团队开发的时候,常常碰到一个问题,就是如何共同管理资源?甚至一个人进行开发的时候如何简化资源的维护,避免无谓的消耗? 如果可以做到在开发的时候,大家把美工做的图片(往往是程序员先自己随 ...
- 面试题思考:Java 8 / Java 7 为我们提供了什么新功能
Java 7 的7个新特性 1.对集合类的语言支持: 2.自动资源管理: 3.改进的通用实例创建类型推断: 4.数字字面量下划线支持: 5.switch中使用string: 6.二进制字面量: 7.简 ...
- "_dns_free_resource_record", referenced from:问题
本文转载至 http://blog.csdn.net/woaifen3344/article/details/41309471 _dns_free_resource_r_dns_free环信SDK集成 ...
- html ; css ; javascript ; json ;
[说明]今天因为看到了前端的js页面和html页面,觉得有必要熟悉一下他们的基本语法,所以花了一天的时间去复习巩固了一下(之前学习过),包括html语法,css语法,javascript语法,对象.B ...
- 160719、Spring + Dubbo + zookeeper (linux) 框架搭建
转载一篇博客,写得不错(至少我参考一下搭建成功了) 转载地址:http://my.oschina.net/wangt10/blog/522799 dubbo简介 节点角色说明: Provider: 暴 ...
- Openstack虚拟机创建流程
续上一篇Openstack安装配置 一,keystone交互认证阶段 1,发送用户名和密码给keystone认证获取token 2,带着token访问nova-api 3,nova-api使用toke ...
- JavaScript四种数值取整方法
一.Math.trunc() 1.定义 Math.trunc()方法去除数字的小数部分,保留整数部分. 2.语法 Math.trunc(value) 3.示例 console.log(Math.tru ...
- C#生成Windows服务
1. 新建一个项目,或者从选择当前解决方案--右键-添加--新建项目 2. 选择Visual C#项目-->Windows 服务,填写要创建的服务名称(修改默认的WindowService1成为 ...