【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 ...
随机推荐
- Spring学习十一----------Bean的配置之基于Java的容器注解@Bean
© 版权声明:本文为博主原创文章,转载请注明出处 @Bean -@Bean标识一个用于配置和初始化一个由SpringIOC容器管理的新对象的方法,类似于XML配置文件的<bean/> -可 ...
- 在Mac OS X中下载Android源代码的一些经验
首先说明.随着最近(2014年6月開始)GFW的升级.这个站点:http://www.android.com/ 已经不能正常訪问了,以下的这些操作均是在我连接VPN的时候进行的. 首先,须要做一些准备 ...
- 在IDEA建立Maven的多模块Web项目
由于要搭建的是Maven项目,考虑到后面可能会有扩展,因此项目搭建的分模块的. 下面一步一步的来搭建这个项目 打开IDEA集成开发环境,点击File ---> New ---> Proje ...
- 有两个好友A和B,住在一片长有蘑菇的由n*m个方格组成的草地,A在(1,1),B在(n,m)。现在A想要拜访B,由于她只想去B的家,所以每次她只会走(i,j+1)或(i+1,j)这样的路线,在草地上有k个蘑菇种在格子里(多个蘑菇可能在同一方格),问:A如果每一步随机选择的话(若她在边界上,则只有一种选择),那么她不碰到蘑菇走到B的家的概率是多少?
第二种方法:首先分析题意,可用概率的方法来计算,做了好几道百度的题目,觉得大多数是再考概率论,所以首先要弄懂题意,最后做题前把公式写出来,这样编码时才能游刃有余. 本题中下面的第一种用迭代枚举的方法来 ...
- PeekMessage究竟做了什么?
1.UI线程 2.工作线程 把Delphi里TThread的WaitFor函数转化成C++代码,就会是下面这个样子. BOOL TThread::WaitFor(HANDLE hThread) { M ...
- 前端几个笔试题及答案(bd)
1. 行内元素.块级元素和空元素(void)举例. 块级元素:<address>.<caption>.<dd>.<div>.<dl>.& ...
- Java语言基础(回头复习)
/* 使用变量的时候要注意的问题: A:作用域 变量定义在哪个大括号内,它就在这个大括号内有效. 并且,在同一个大括号内不能同时定义同名的变量. B:初始化值 没有初始化值的变量不能直接使用. 你只要 ...
- 转 EBP ESP 的理解
PS:EBP是当前函数的存取指针,即存储或者读取数时的指针基地址:ESP就是当前函数的栈顶指针.每一次发生函数的调用(主函数调用子函数)时,在被调用函数初始时,都会把当前函数(主函数)的EBP压栈,以 ...
- 【Java 语言生态篇】Junit 测试单元
01 概述 JUnit是一个由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework).Junit测试是白盒测试.JUn ...
- [HDU4609] 3-idiots FFT+计数
用FFT再去重计算出两条边加起来为某个值得方案数,然后用总方案数减去不合法方案数即可. #include<iostream> #include<cstdio> #include ...