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. c++ assert

    #include<iostream> #include <assert.h> using namespace std; int main() { ; assert(a == ) ...

  2. 引擎设计跟踪(九.14.2a) 导出插件问题修复和 Tangent Space 裂缝修复

    由于工作很忙, 近半年的业余时间没空搞了, 不过工作马上忙完了, 趁十一有时间修了一些小问题. 这次更新跟骨骼动画无关, 修复了一个之前的, 关于tangent space裂缝的问题: 引擎设计跟踪( ...

  3. 引擎设计跟踪(九.8) Gizmo helper实现与多国语言

    最近把gizmo helper的绘制做好了. 1.为了复用代码,写了utility来创建sphere, cube, cylinder, plane, ring(line), circle(solid) ...

  4. ASP.NET 应用程序安全

    原文:http://msdn.microsoft.com/zh-cn/magazine/hh708755.aspx 一.跨站点脚本 简介 XSS 攻击是指将脚本恶意注入用户的浏览会话,这通常在用户不知 ...

  5. jdom处理的XML Document 和String 之间的相互转化

    package util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; ...

  6. java基础知识回顾之javaIO类---BufferedReader和BufferedWriter

    使用了装饰设计模式:此类的设计是为了提高流操作数据的效率.思想就是定义容器将数据进行临时存储,对于缓冲区对象,其实就是将这个容器进行了分装,并提供了更高效的操作方法. BufferReader: pa ...

  7. MySQL Date 函数

    MySQL Date 函数 下面的表格列出了 MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 ...

  8. NGINX轻松管理10万长连接 --- 基于2GB内存的CentOS 6.5 x86-64

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=190176&id=4234854 一 前言   当管理大量连接时,特别 ...

  9. web快速开发c/s软件构架

    很久没用.net winform 做东西,对控件相对比较陌生,另外控件的UI也不是那么好改.公司项目需要有web客户端,同时有软件客户端形式.考虑再三采用webBrowser+html 来实现 .用h ...

  10. Python之socketserver源码分析

    一.socketserver简介 socketserver是一个创建服务器的框架,封装了许多功能用来处理来自客户端的请求,简化了自己写服务端代码.比如说对于基本的套接字服务器(socket-based ...