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. ...
随机推荐
- 用Visual Studio高版本号打开低版本号的project,转换时出现错误:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
解决方法是: 在电脑里面搜索发现 C:\Program Files\Microsoft Visual Studio 10.0\VC\bin C:\Windows\winsxs\x86_netf ...
- Lucene中TokenStream,Tokenizer,TokenFilter,TokenStreamComponents与Analyzer
TokenStream extends AttributeSource implements Closeable: incrementToken,end,reset,close Tokenizer直接 ...
- C++标准I/O库:iostream, fstream, sstringstream
在写代码的过程中.我们最常做的事就是io操作,不管是对控制台,还是文件.但一段时间不写代码就忘了,这里理一下C++标准I/O库的详细类和操作. C++的标准I/O库包含我们常常使用的iostream, ...
- sparse-PCA(稀疏主成分分析)是什么?
不多说,直接上干货! 复杂降维技术有spare-PCA和sparse coding. 最近在科研需要,感谢下面的博主. Sparse PCA 稀疏主成分分析
- 【iOS系列】-autorelease的作用
内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease 1:对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对 ...
- 动态输出html一些效果失效的处理
利用AJAX动态加载页面,实现无刷新加载,有时会出现一些问题.比如说,在一些jquery控件中,是利用在页面加载的时候,对一些带有特殊属性的元素进行处理,比如事件绑定什么的.假如是动态加载,此时页面早 ...
- XMU 1050 Diffuse Secret 【最短路】
1050: Diffuse Secret Time Limit: 500 MS Memory Limit: 64 MBSubmit: 10 Solved: 8[Submit][Status][We ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- Android NDK生成及连接静态库与动态库
对于Android应用开发,大部分情况下我们使用Java就能完整地实现一个应用.但是在某些情况下,我们需要借助C/C++来写JNI本地代码.比如,在使用跨平台的第三方库的时候:为了提升密集计算性能的时 ...
- phpstorm更改sql文件匹配类型
正常情况下,sql文件都有对应的文件类型.但是默认的sql文件只是关联普通的sql.很多语法都无法高亮,以及自动提醒.