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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. 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. ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

  10. 【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 ...

随机推荐

  1. 用npm安装express后express命令找不到

    Windows 平台加了 npm install -g express 也不行AppData\Roaming\npm 下面没有 express.bat 解决办法: sudo npm install - ...

  2. 异步任务(AsyncTask)

    1.Android UI组件更新简介 Android的UI线程主要负责处理用户的按键事件.用户触屏事件及屏幕绘图事件等,因此开发者的其它操作不应该,也不能阻塞UI线程,否则UI界面将会变的停止响应.A ...

  3. IT架构之IT架构标准——思维导图

    参考: [日] 野村综合研究所系统咨询事业本部. 图解CIO工作指南. 周自恒译 人民邮电出版社,2014

  4. D3D depth buffer的预览

    在使用D3D开发游戏的过程中,很多情况下都会用到depth buffer来完成特定的效果,比如DOF,Shadows,SSAO等等.在这些情况下我们就可能需要预览depth buffer来确定它是正确 ...

  5. 使用angular.js开发的一个简易todo demo

    前沿 在CVTE实习考察的一周里,接触到了angular,并在最后的一天任务里要求使用angular做一个功能主要包括创建.编辑.恢复.删除以及留言的todo demo,并支持响应式布局.因为之前没怎 ...

  6. POJ 1144 Network(Tarjan)

    题目链接 题意 : 找出割点个数. 思路 : Tarjan缩点,u是割点的充要条件是:u要么是具有两个以上子女的深度优先生成树的根,要么不是根,而有一个子女v满足low[v]>=dfn[u]. ...

  7. Tomcat SSL 设置

    1. 先用如下命令生成tomcat 证书 cls rem please set the env JAVA_HOME before run this bat file SET JAVA_HOME=C:\ ...

  8. lintcode:带环链表

    带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...

  9. 4 tips for staying productive on Friday

    4 tips for staying productive on Friday如何让你的周五和周一一样有效率1.Schedule Your Day with Tasks 用任务计划一天 Sometim ...

  10. CentOS系统常用命令

    1.进入目录命令 cd /usr/bin //进入etc目录 cd .. //切换到上一层目录 cd / //进入主目录 2.新建文件命令 比如进入某个目录后执行 vim test 2.查看运行的项目 ...