题目:

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. HDU BestCoder Round #1 1002 项目管理

    项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. php设计模式中的类型安全 指--只接受特定的对象 ---以避免发生错误

    在百度百科中---类型安全代码指访问被授权可以访问的内存位置

  3. JVM学习02-GC算法与种类

    1. GC 简单介绍 GC(Garbage Collection) 是垃圾收集的简写,GC机制是java中一个比較重要的概念.java的内存管理提供了内存的分配和释放.内存处理是程序编写人员非常eas ...

  4. Java结束线程的三种方法

    线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁,不能再次start,只能重新建立新的线程对象,但有时run()方法是永远不会结束的.例如在程序中使用线程进行So ...

  5. 1-1:CSS3课程入门之属性选择器

    div[name=jewave] 选取属性名为name且属性值是"jewave"的元素 div[name^=jewave]选取属性名为name且属性值以"jewave&q ...

  6. 动态内存分配(Dynamic memory allocation)

    下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题.    尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...

  7. IntelliJ IDEA打可执行jar包

    <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <config ...

  8. <转> Struct 和 Union区别 以及 对内存对齐方式的说明

    转载地址:http://blog.csdn.net/firefly_2002/article/details/7954458 一.Struct 和 Union有下列区别: 1.在存储多个成员信息时,编 ...

  9. Runnable 和 Callable的区别

    Runnable 与 Callable的区别: (1)Callable规定的方法是call(),Runnable规定的方法是run(). (2)Callable的任务执行后可返回值,而Runnable ...

  10. [转]C#中的结构体与类的区别

    C#中的结构体与类的区别   经常听到有朋友在讨论C#中的结构与类有什么区别.正好这几日闲来无事,自己总结一下,希望大家指点. 1. 首先是语法定义上的区别啦,这个就不用多说了.定义类使用关键字cla ...