【Lintcode】087.Remove Node in Binary Search Tree
题目:
Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.
Given binary search tree:
5
/ \
3 6
/ \
2 4
Remove 3, you can either return:
5
/ \
2 6
\
4
or
5
/ \
4 6
/
2
题解:
这个题就是考察对二叉树操作的熟练程度,没有多少技巧,下面的程序中唯一能算作技巧的是更换node时有时并不需要切断其与左右节点和父节点的链接,只需要更换val值就可以了。
Solution 1 ()
class Solution {
public:
TreeNode* removeNode(TreeNode* root, int value) {
if (root == NULL)
return NULL;
TreeNode * head = new TreeNode();
head->left = root;
TreeNode * tmp = root, *father = head; while (tmp != NULL) {
if (tmp->val == value)
break;
father = tmp;
if (tmp->val > value)
tmp = tmp->left;
else
tmp = tmp->right;
}
if (tmp == NULL)
return head->left; if (tmp->right == NULL) {
if (father->left == tmp)
father->left = tmp->left;
else
father->right = tmp->left;
} else
if (tmp->right->left == NULL) {
if (father->left == tmp)
father->left = tmp->right;
else
father->right = tmp->right; tmp->right->left = tmp->left; } else {
father = tmp->right;
TreeNode * cur = tmp->right->left;
while (cur->left != NULL) {
father = cur;
cur = cur->left;
}
tmp->val = cur->val;
father->left = cur->right;
}
return head->left;
}
};
用右子树最小值替代
Solution 2 ()
class Solution {
public:
TreeNode* removeNode(TreeNode* root, int value) {
if (root == nullptr) {
return nullptr;
}
if (root->val > value) {
root->left = removeNode(root->left, value);
} else if (root->val < value) {
root->right = removeNode(root->right, value);
} else {
// leaf node
if (root->left == nullptr && root->right == nullptr) {
root = nullptr;
} else if (root->left == nullptr) {
root = root->right;
} else if (root->right == nullptr) {
root = root->left;
} else {
TreeNode* tmp = findMin(root->right);
root->val = tmp->val;
root->right = removeNode(root->right, tmp->val);
}
} return root;
} TreeNode* findMin(TreeNode* root) {
while (root->left != nullptr) {
root = root->left;
}
return root;
}
};
用左子树最大值替代
Solution 3 ()
class Solution {
public:
TreeNode* removeNode(TreeNode* root, int value) {
if (root == nullptr) {
return nullptr;
}
if (root->val > value) {
root->left = removeNode(root->left, value);
} else if (root->val < value) {
root->right = removeNode(root->right, value);
} else {
// leaf node
if (root->left == nullptr && root->right == nullptr) {
root = nullptr;
// only one child
} else if (root->left == nullptr) {
root = root->right;
} else if (root->right == nullptr) {
root = root->left;
// two child
} else {
TreeNode* tmp = findMax(root->left);
root->val = tmp->val;
root->left = removeNode(root->left, tmp->val);
}
} return root;
} TreeNode* findMax(TreeNode* root) {
while (root->right != nullptr) {
root = root->right;
}
return root;
}
};
【Lintcode】087.Remove Node in Binary Search Tree的更多相关文章
- Lintcode: Remove Node in Binary Search Tree
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...
- Remove Node in Binary Search Tree 解答
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...
- 【leetcode】Convert Sorted List to Binary Search Tree
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 【PAT】1043 Is It a Binary Search Tree(25 分)
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- 【leetcode】701. 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, in ...
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【树】Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
随机推荐
- caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel
c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...
- Iconfot阿里妈妈-css高级应用
矢量图标替换教程 首先进入:http://www.iconfont.cn/ 搜索你分类的关键字---然后加入购物车 加入购物车之后,下载到本地用浏览器打开demo.html 把a class=“原来样 ...
- EntityFramework 6.0 修改一个已经存在的对象
public void UpdateObj(someobject obj) { db.Entry(obj).State = EntityState.Modified; db.SaveChanges() ...
- mybatis的拦截器及分页机制
https://blog.csdn.net/ssuperlg/article/details/79847889
- JavaWeb、J2-SE开发框架——Spring
相关博客: 2.spring官网 1.使用Spring的jdbcTemplate进一步简化JDBC操作
- js关于变量作为if条件的真假问题
var a = ""; if(a){ ..... }else{ .....} 以下情况会被认为返回false: "" 空的字符串 为 0 的数字 为 null ...
- /usr/bin/mysqld_safe_helper: Cannot change uid/gid (errno: 1) (转)
From: https://www.rootusers.com/how-to-fix-mariadb-10-0-29-selinux-update-failure/ 安装mysql 10.0.29后, ...
- ssh无密码登陆屌丝指南
[0]写在前面 由于ssh 实现的是免密码登陆,大致步骤是: 0.1) client通过ssh登陆到server: 0.2) server检查家目录下的.ssh文件, 并发送公钥文件 authoriz ...
- 奇妙的go语言(開始篇)
[ 声明:版权全部.欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 从前接触脚本语言不多,可是自从遇到go之后,就開始慢慢喜欢上了这个脚本语言.go语言是goog ...
- iOS判断为空或者只为空格
本文转载至 :http://www.cnblogs.com/superhappy/archive/2012/11/08/2761403.html 经常有需求 要判断不能为空,后台老是鄙视不做非空判断的 ...