Delete a node from 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode deleteNode(TreeNode root, int val) {
if (root == null) return root; if (root.val > val) {
root.left = deleteNode(root.left, val);
} else if (root.val < val) {
root.right = deleteNode(root.right, val);
} else {
if (root.left == null && root.right == null) {
return null;
} else if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
} else {
root.val = findMin(root.right).val;
root.right = deleteNode(root.right, root.val);
}
}
return root;
} public TreeNode findMin(TreeNode n) {
if (n.left != null) {
return findMin(n.left);
}
return n;
}
}
Delete a node from BST的更多相关文章
- [Algorithm] Delete a node from Binary Search Tree
The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...
- BST 解析 (二)height and deletion
前面一章介绍了BST的结构和一些简单的基本功能,例如:insert,findMin,nextLarger等等.这一节主要讲解一些BST的delete node操作还有BST的height的分析以及一些 ...
- [LeetCode] 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: 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 ...
- [Swift]LeetCode450. 删除二叉搜索树中的节点 | 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 ...
- 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 ...
随机推荐
- 彻底解决Ubuntu 14.04 重启后DNS配置丢失的问题
最近得到一个比较好用的DNS,每次重启后都修改DNS配置文件 /etc/resolv.conf 重启就会失效 从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvcon ...
- Visual Studio Code 怎么支持中文
这其实是编码问题 和字体什么的没有关系,在VSCode中默认是用UTF-8编码方式打开文件,只要你的文件是这个编码就能正常打开如果不是,不进行设置VSCode是不会自动切换编码方式打开 手动可以Reo ...
- JAVA 注解的几大作用及使用方法详解
JAVA 注解的几大作用及使用方法详解 (2013-01-22 15:13:04) 转载▼ 标签: java 注解 杂谈 分类: Java java 注解,从名字上看是注释,解释.但功能却不仅仅是注释 ...
- Open Yale course:Listening to Music
一.Introductionhttps://app.yinxiang.com /Home.action?offer=www_menu#n=4b034a29-986d-4914-8220-eb99c2e ...
- PHP中的NULL类型
特殊的NULL值表示一个变量没有值,NULL类型唯一的值就是NULL.我们需要注意的是NULL不表示空格,也不表示零,也不是空字符串,而是一个变量的值为空.NULL不区分大小写,在下列情况下一个变量被 ...
- addEventListener和on的区别
为什么需要addEventListener? 先来看一个片段: html代码 <div id="box">追梦子</div> 用on的代码 1 window ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- python文件I/O(转)
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- C语言:break和continue
break: break用于循环则是终止循环,break如果用于switch,则是用于终止switch.break不能直接用于if,除非if是属于循环内部的一个子句 ;i<;i++) { > ...
- 软件测试-----Graph Coverage作业
/******************************************************* * Finds and prints n prime integers * Jeff ...