【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 ...
随机推荐
- jquery插件中使用ajax并且获取使用插件的对象
jquery插件中使用ajax后无法在里面获取this 解决办法是在函数内使用ajax前声明变量 $this=this 然后再ajax中使用$this
- LinkedList 基本示例及源码解析
目录 一.JavaDoc 简介 二.LinkedList 继承接口和实现类介绍 三.LinkedList 基本方法介绍 四.LinkedList 基本方法使用 五.LinkedList 内部结构以及基 ...
- java 中的CountDownLatch
直接使用thread可以使用thread和wait notify 实现顺序执行 线程池中可以使用CountDownLatch 进行顺序执行 package com.test; import java. ...
- GIT简单使用——多人协作篇
多人协作的工作模式通常是这样:1.首先,可以试图用git push origin <branch-name>推送自己的修改:2.如果推送失败,则因为远程分支比你的本地更新,需要先用git ...
- 【Python】selenium调用IE11浏览器,报错“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window
当编写自动化脚本,定位浏览器元素时,报如下错误: 代码: >>> # coding=utf-8 >>> from selenium import webdriver ...
- WampServer无法直接打开myprojects的解决方法
https://jingyan.baidu.com/article/7e4409533ace042fc1e2ef40.html
- 4276: [ONTAK2015]Bajtman i Okrągły Robin
4276: [ONTAK2015]Bajtman i Okrągły Robin Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 345 Solved ...
- 【BZOJ4945】[Noi2017]游戏 2-SAT
[BZOJ4945][Noi2017]游戏 题目描述 题解:2-SAT学艺不精啊! 这题一打眼看上去是个3-SAT?哎?3-SAT不是NPC吗?哎?这题x怎么只有8个?暴力走起! 因为x要么不是A要么 ...
- 九度OJ 1035:找出直系亲属 (二叉树、递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...
- exception_action
for i in range(3, -2, -1): try: print(4 / i) except Exception as e: print(Exception) print(e)