[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 ...
随机推荐
- consistent.go 源码阅读
) > len(c.circle) { hashes = nil } for k := range c.circle { hashes = app ...
- Golang 知识点总结
Golang 知识点总结 目录 [−] 各种类型复制的时候的花费 可使用内建函数的类型 (len.cap.close.delete.make) 内建容器类型的值比较 组合类型T{...}的值比较 零值 ...
- 【源码】otter工程结构
最近在搞数据同步相关的内容,需要对otter的代码进行扩展,所以需要先熟悉一下otter的源码.首先我们整体来看下otter的工程结构.otter的工程结构比较复杂,需要花费一定的时间来理解各个部分的 ...
- BZOJ1467_Pku3243 clever Y_EXBSGS
BZOJ1467_Pku3243 clever Y_EXBSGS Description 小Y发现,数学中有一个很有趣的式子: X^Y mod Z = K 给出X.Y.Z,我们都知道如何很快的计算K. ...
- java面试题总结
一.http://blog.csdn.net/moneyshi/article/details/50786786 二.http://blog.csdn.net/moneyshi/article/det ...
- selenium IDE中log的保存与查看方法
下载selenium IDE的log保存插件为File Logging(selenium IDE),下载安装方式如下: (1)打开firefox浏览器----点击右上角----附加组件---插件--- ...
- go.js remove 特定part
我想删除一个实际上是背景的部分. 假设这是我添加part的方式 myDiagram.add( scope.gj(go.Part,//此Part未绑定到任何模型数据 { layerName:“Backg ...
- 自定义超链接动画---transition
效果图: <a href="#"> <span>HTML</span> </a> a { position: relative; t ...
- HTML入门知识汇总
1. HTML认识 1.1 什么是HTML HTML是描述(制作)网页的语言,指的是超文本标记语言(Hyper Text Markup Language). 超文本:就是指页面内可以包含图片.链接.甚 ...
- 学习 day4 html 盒子模型
盒子模型 1.框模型 框:页面上所有元素都可以称为“框” 框模型:(BOX Model),又称盒子模型 定义框处理元素内容.内边距padding.外边距margin.边框的样式border 外边距ma ...