[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. 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
- 思路:分两种情况,一种是要删除的节点只有一个孩子,另一种是要删除的节点有两个孩子;
如果只有一个孩子,那么我们让这个节点引用到他非空的孩子上去,即root = root.right或
root = root.left;如果有两个孩子的话,那么有两种办法,一种是找他右边的最小值,另一种
是找他左边的的最大值。将值赋给该节点,然后删除右边最小值或左边最大值;
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public TreeNode deleteNode(TreeNode root, int key) {
- if (root==null)
- return null;
- else if (key<root.val)
- root.left = deleteNode(root.left,key);
- else if (key>root.val)
- root.right = deleteNode(root.right,key);
- else {
- if (root.left!=null&&root.right!=null){
- TreeNode tmp = findMin(root.right);
- root.val = tmp.val;
- root.right = deleteNode(root.right,root.val);
- }
- else {
- TreeNode tmp = root;
- if (root.left==null)
- root = root.right;
- else if (root.right==null)
- root = root.left;
- tmp = null;
- }
- }
- return root;
- }
- private TreeNode findMin(TreeNode root){
- if (root==null)
- return null;
- if (root.left==null)
- return root;
- return findMin(root.left);
- }
- }
[Leetcode]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二叉搜索树删除节点
二叉树变量只是一个地址 public static void main(String[] args) { TreeNode t = new TreeNode(3); help(t); System.o ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 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 ...
- 【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. ...
- 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 ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
随机推荐
- Java 读书笔记 (十七) Java 重写(Override)与重载(Overload)
重写(Override) 重写是子类对父类的允许访问的方法的实现过程重新编写,返回值和形参都不能改变,即外壳不变,核心重写. // 如果重写不是相当于重新定义了一个方法?那为什么不直接写,还要exte ...
- 浅谈cookie,sessionStorage和localStorage
cookie:cookie在浏览器和服务器间来回传递 cookie数据不能超过4k 同时每次http请求都会携带cookie,所以cookie只适合保存很小的数据,比如会话标识 cookie只在设置的 ...
- GIT的使用流程
GIT的使用流程 1 github注册流程 1 进入github官网:https://github.com/ 2 注册一个自己的github账号 3 右上角选择New repository 4 进入c ...
- Django中用户权限模块
Django中用户权限模块 1 auth模块 auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管理系 ...
- 【爆料】-《布莱顿大学毕业证书》Brighton一模一样原件
布莱顿大学毕业证[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&am ...
- VMware workstation创建虚拟机console图文
1. 概述2. 配置入口3. 新建虚拟机向导3.1 类型配置3.2 硬件兼容性3.3 操作系统安装3.4 客户机操作系统类型3.5 客户机的名称位置4. 客户机硬件配置选择4.1 客户机处理器配置4. ...
- 微信jssdk config:invalid signature 签名错误 ,问题排查过程
invalid signature签名错误.建议按如下顺序检查: 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisi ...
- 隐马尔可夫模型(HMM)总结
摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项(算法过程,调参等注意事项) 5.实现和具体例子 6.适用场合 内容: 1.算法概述 隐马尔科夫模型(Hidden Markov ...
- 系统的讲解 - PHP 浮点数高精度运算
目录 概述 浮点数运算的"锅" 任意精度数学函数 常用数值处理方案 扩展 小结 概述 记录下,工作中遇到的坑 ... 关于 PHP 浮点数运算,特别是金融行业.电子商务订单管理.数 ...
- linux安装nvm node版本管理器 nvm常用命令 部署node服务器环境
1,nvm git地址点击打开链接,安装命令 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh ...