delete-node-in-a-bst
https://leetcode.com/problems/delete-node-in-a-bst/
/**
* 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 == NULL) {
return NULL;
} TreeNode* cur = root;
TreeNode* last = NULL;
while (cur != NULL && cur->val != key) {
if (cur->val > key) {
last = cur;
cur = cur->left;
}
else {
last = cur;
cur = cur->right;
}
} if (cur == NULL) {
return root;
} TreeNode* tlast = cur;
TreeNode* tcur;
if (cur->left != NULL) {
tcur = cur->left;
while (tcur->right != NULL) {
tlast = tcur;
tcur = tcur->right;
} if (tcur == tlast->left) {
tlast->left = tcur->left;
}
else {
tlast->right = tcur->left;
}
cur->val = tcur->val;
}
else if (cur->right != NULL) {
tcur = cur->right;
while (tcur->left != NULL) {
tlast = tcur;
tcur = tcur->left;
}
if (tcur == tlast->left) {
tlast->left = tcur->right;
}
else {
tlast->right = tcur->right;
}
cur->val = tcur->val;
}
else {
if (last == NULL) {
// only root
return NULL;
}
if (cur == last->left) {
last->left = NULL;
}
else if (cur == last->right){
last->right = NULL;
}
else {
// error
}
}
return root;
}
};
delete-node-in-a-bst的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 【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 ...
随机推荐
- CHtml::radioButtonList
public function getSortList(){ $arr = array(); $arr[0]['id']=0; $arr[0]['name']="否"; $arr[ ...
- 简单shell脚本
简单shell脚本备忘 #!/bin/sh num= ] do table_num=`printf %03d ${num}` echo album_info_${table_num} #mys ...
- Codeforces Round #242 (Div. 2) C题
题目链接:http://codeforces.com/contest/424/problem/C, 想来一个小时,就是做不出,都做出来了,悲剧! 分析:我们知道交换异或的顺序不影响答案! 然后就是求t ...
- 移动MM failed to find resource file{mmiap.xml}
原地址:http://blog.csdn.net/alking_sun/article/details/36175861 在进行移动MM集成的时候总是会遇到一个bug: failed to find ...
- http authorization basic请求代码示例
/** * */ package testJava.java; import java.io.BufferedReader; import java.io.InputStream; import ja ...
- 13test02:信用卡校验
/*#include<iostream> using namespace std; void input(); int counter=0,jishu_sum=0,oushu_sum=0, ...
- iptables使用multiport 添加多个不连续端口 不指定
iptables使用multiport 添加多个不连续端口 碟舞飞扬 , 01:26 , Linux技术 , 评论(0) , 引用(0) , 阅读(12214) , Via 本站原创 大 | 中 ...
- 安装微软ASP.NET MVC 4,运行以下的包管理器控制台命令
(菜鸟,勿喷,有错求指正)Asp.net 新建的类库中安装MVC4 .下面是步骤,1+2:打开程序包管理控制台,3:运行Install-Package Microsoft.AspNet.Mvc - ...
- 关于android的分辨率
关于Android的分辨率支持,为大家翻译官方文档 看世界杯的空闲时间,翻译一下官方文档.分辨率问题是大家都很关心的(720×480会不会悲剧),而关于这个问题,android官方的文档无疑最有说服力 ...
- Spark源码分析(一)-Standalone启动过程
原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3858065.html 为了更深入的了解spark,现开始对spark源码进行分析,本系列文章以spark ...