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. 全球SEO行业调查报告

    这是一份来自MOZ的调查报告,本报告是两年一次的SEO行业调查,主要围绕SEO从业人员的特征.工作内容时间分配比例.对未来市场的看法.使用的seo工具以及SEO知识扩充渠道等展开. 这份报告可以对从事 ...

  2. win8 获取管理员权限

    Win8 下动不动 就弹出要管理员权限什么.......   网上找到很多方法.  什么注册表什么..... 不行..   以下这个方法可行. 按WIN+R,运行对话框中输入gpedit.msc,开启 ...

  3. 帝国cms无法注册登录快速解决方法 附路径

    帝国cms安装相对比较简单,一路next,一般从网上下载的系统都会带一些数据,恢复备份数据后,清除缓存,更新数据,一个copy版的网站就出来了.但是为了se的友好需要改动很多地方,不然很容易被认为是s ...

  4. C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. swappiness

    在ubuntu 里面,swappiness的值的大小对如何使用swap分区是有着很大的联系的.swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=10 ...

  6. Oracle自带的用户

    Oracle安装完毕创建数据库实例的时候,会自动生成三个用户sys,system,scott. sys用户是超级管理员,具有最高权限,充当sysdba角色,可以执行create database,默认 ...

  7. WCF 传输和接受大数据

    向wcf传入大数据暂时还没找到什么好方案,大概测了一下传输2M还是可以的,有待以后解决. 接受wcf传回的大数据,要进行web.config的配置,刚开是从网上搜自己写进行配置,折磨了好长时间. 用以 ...

  8. Xcode 创建静态库和动态库

    1.linux中静态库和动态库区别: 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态库和动态库两种. 静态库:这类库的名字一般是libxxx.a:利用静态函数库编译成的文件 ...

  9. mysql之游标

    游标 行,也不存在每次一行地处理所有行的简单方法(相对于成批地处理它们).有时,需要在检索出来的行中前进或后退一行或多行.这就是使用游标的原因.游标( cursor)是一个存储在MySQL服务器上的数 ...

  10. 李洪强iOS开发之下载

    // // //  LHQDownLoader.m //  A21 - 李洪强 - 下载 // //  Created by vic fan on 16/7/3. //  Copyright © 20 ...