题目:

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的更多相关文章

  1. 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. ...

  2. Remove Node in Binary Search Tree 解答

    从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...

  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 ...

  4. 【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 ...

  5. 【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 ...

  6. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【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 ...

  9. 【树】Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

随机推荐

  1. 分区容量大于16TB的格式化

    File systems do have limits. Thats no surprise. ext3 had a limit at 16 TB file system size. If you n ...

  2. Chrome自带恐龙小游戏的源码研究(四)

    在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...

  3. python爬虫获取百度图片(没有精华,只为娱乐)

    python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...

  4. 移动应用开发测试工具Bugtags集成和使用教程【转载】

    前段时间,有很多APP突然走红,最终却都是樱花一现.作为一个创业团队,突然爆红是非常难得的机会.然并卵,由于没有经过充分的测试,再加上用户的激增,APP闪退.服务器数据异常等问题就被暴露出来,用户的流 ...

  5. Spark源码分析之六:Task调度(二)

    话说在<Spark源码分析之五:Task调度(一)>一文中,我们对Task调度分析到了DriverEndpoint的makeOffers()方法.这个方法针对接收到的ReviveOffer ...

  6. php解析带有命名空间的xml

    xml如果带有命名空间我们将如何解析,例如: <ns1:CreateBillResponse xmlns:ns1="http://neusoft.com" xmlns:xsd ...

  7. C#中图片.BYTE[]和base64string的转换

    在C#中 图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath);                MemoryStream ms = ...

  8. 自定义一个处理图片的HttpHandler

    有时项目里我们必须将图片进行一定的操作,例如水印,下载等,为了方便和管理我们可以自定义一个HttpHander 来负责这些工作 后台: public class ImageHandler : IHtt ...

  9. zookeeper启动流程简单梳理

    等着測试童鞋完工,顺便里了下zookeeper的启动流程 zk3.4.6 启动脚本里面 nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_ ...

  10. delphi视频聊天

    用Delphi开发视频聊天软件 一.引言 我们知道视频聊天软件的关键技术在于采集视频,并实时传输给聊天软件在线的人.对于视频的采集,这里采用微软公司的关于数字视频的一个软件包VFW(Video for ...