450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key)
{
if (!root)
{
return nullptr;
}
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 || !root->right)
{
root = (root->left) ? root->left : root->right;
}
else
{
TreeNode *cur = root->right;
while (cur->left)
{
cur = cur->left;
}
root->val = cur->val;
root->right = deleteNode(root->right, cur->val);
}
}
return root;
}
};
参考:https://www.cnblogs.com/grandyang/p/6228252.html
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] 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 ...
- Java实现 LeetCode 450 删除二叉搜索树中的节点
450. 删除二叉搜索树中的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引 ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- LeetCode701 二叉搜索树中插入结点
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 保证原始二叉搜索树中不存在新值. 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜 ...
- [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 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 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- Deepin-安装vscode
安装方式有两种: 1.通过命令安装 sudo apt-get install vscode 2.通过deb或rpm包安装 我们是Debian系列的系统,所以用deb包,关于红帽系统,请使用rpm包. ...
- ecshop广告宽度值必须在1到1024之间的解决方法
ecshop加广告出现广告位的宽度值必须在1到1024之间的解决方法,这个问题是今天刚刚发现的,我就郁闷了,如今1024宽度的广告能做什么.你看看京东,天猫,非常多都是大型的横幅广告,这点ecshop ...
- 【iOS系列】-iOS中内存管理
iOS中创建对象的步骤: 1,分配内存空间,存储对象 2,初始化成员变量 3,返回对象的指针地址 第一:非ARC机制: 1,对象在创建完成的同时,内部会自动创建一个引用计数器,是系统用来判断是否回收对 ...
- 一个JS引发的跨域问题
忽然遇上跨域错误. 我们有张页面,使用了EXT.js,在本地运行正常,部署到服务器上,出不来数据.F12调试,提示有跨域错误? XMLHttpRequest cannot load http://19 ...
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- YTU 2402: Common Subsequence
2402: Common Subsequence 时间限制: 1 Sec 内存限制: 32 MB 提交: 63 解决: 33 题目描述 A subsequence of a given seque ...
- 《Visual C++ 2010入门教程》系列二:安装、配置和首次使用VS2010
作者:董波 日期:2010.6.15 写在前面 在我还在上学的时候,我选择了C++,最初我用VC6作为我的IDE,我看过很多本C++的教材,有的适合我,有的不适合我,其中有一本叫<Visual ...
- POJ - 2418 Hardwood Species(map,trie,BST)
1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...
- 源代码管理工具SVN
1.源代码管理工具概述 2_SVN常用指令.hm Checkout把整个项目所有的源代码从服务器下载到本地 Update:将服务器上的代码更新到本地(只会更新被修改的文件) Commit:将本地的修改 ...