从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论。

这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况。

一种简单些的思维是只考虑删除节点是否有右子树(因为第一个比删除节点大的节点可能出现在右子树,不会出现在左子树)。

这里用Target表示删除节点,Parent表示删除节点的母节点。

1. 没有右子树

Parent.left / Parent.right = Target.left

2. 有右子树

1) 找到第一个比删除节点大的节点Node

2) 删除该节点Node

3) Target.val = Node.val 或 保留 Node 删除Target

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of the binary search tree.
* @param value: Remove the node with given value.
* @return: The root of the binary search tree after removal.
*/
public TreeNode removeNode(TreeNode root, int value) {
// write your code here
TreeNode dummy = new TreeNode(0);
dummy.left = root;
TreeNode parent = findNodeParent(dummy, root, value);
TreeNode target;
if (parent.left != null && parent.left.val == value) {
target = parent.left;
} else if (parent.right != null && parent.right.val == value) {
target = parent.right;
} else {
return dummy.left;
}
deleteNode(parent, target);
return dummy.left;
} private TreeNode findNodeParent(TreeNode parent, TreeNode node, int val) {
if (node == null || node.val == val) {
return parent;
}
if (node.val < val) {
return findNodeParent(node, node.right, val);
} else {
return findNodeParent(node, node.left, val);
}
} private void deleteNode(TreeNode parent, TreeNode target) {
if (target.right == null) {
if (parent.right == target) {
parent.right = target.left;
} else {
parent.left = target.left;
}
} else {
// Find first element that is greater than target
TreeNode node1 = target.right;
TreeNode node2 = target;
while (node1.left != null) {
node2 = node1;
node1 = node1.left;
}
// Remove node1
if (node1 == node2.left) {
node2.left = node1.right;
} else {
node2.right = node1.right;
}
// Remove target
if (target == parent.right) {
parent.right = node1;
} else {
parent.left = node1;
}
node1.left = target.left;
node1.right = target.right;
}
}
}

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

  3. [Algorithm] Delete a node from Binary Search Tree

    The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...

  4. Lowest Common Ancestor of a Binary Search Tree 解答

    Question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes ...

  5. Validate Binary Search Tree 解答

    Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d ...

  6. 数据结构基础---Binary Search Tree

    /// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and sea ...

  7. Binary Search Tree Iterator 解答

    Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized wi ...

  8. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  9. LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder

    1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...

随机推荐

  1. Windows7 64位系统搭建Cocos2d-x 2.2.1最新版以及Android交叉编译环境(具体教程)

    原文地址:http://blog.csdn.net/sttyytw/article/details/17005263 声明:本教程在參考了下面博文,并经过自己的摸索后实际操作得出,本教程系本人原创,因 ...

  2. 窥探 Swift 之 函数与闭包的应用实例

    今天的博客算是比较基础的,还是那句话,基础这东西在什么时候 都是最重要的.说到函数,只要是写过程序就肯定知道函数是怎么回事,今天就来讨论一下Swift中的函数的特性以及Swift中的闭包.今天的一些小 ...

  3. Java基础知识强化92:日期工具类的编写和测试案例

    1. DateUtil.java,代码如下: package cn.itcast_04; import java.text.ParseException; import java.text.Simpl ...

  4. centos5.5上apache快速安装H264流媒体支持MP4-H264边下边播

    2013年的某一天,客户反馈北京同事做的广告视频下载速度好慢,几MB的视频在手机上要下载接近一分钟才能开始播放. 我分析后发现两点:1)托管的服务器没支持流媒体:2)广告视频MP4并非流媒体格式. 对 ...

  5. eclipse 库 library jar包 工程 总结

    引用库错误 如果在libraries中发现有小红叉,表明引用库错误 解决办法:在左侧projects中add引用到的库 如:我们的支付库引用了以下三个库 那么需要在projects中add这三个库   ...

  6. javascript MD5

    var MD5 = function (string) { function RotateLeft(lValue, iShiftBits) { return (lValue<<iShift ...

  7. 一条insert语句批量插入多条记录

    一条insert语句批量插入多条记录 常见的insert语句,向数据库中,一条语句只能插入一条数据: insert into persons (id_p, lastname , firstName,  ...

  8. parent.location.href和location.href区别

    parent.location.href='ind.php'parent用于框架结构,需要全网页转向如果你的网页是左右框架,那么,直接把当前页面全部转到ind.php中 location.href=' ...

  9. myql查询创建表语句SHOW CREATE TABLE table_name

    技术背景:刚开始学习MySQL时候,有时偷懒,会用SHOW CREATE TABLE 表名\G来复制表创建语句,可是当运行的时候总会因为"表名和列名上有单引号",提示语法错误不能运 ...

  10. 自定义异常throw

    简单自定义一个年龄小于等于0,或者大于120会出现的异常 首先继承父类Exception,调用父类的构造器,这样才可以报出自己想要的异常 public class AgeException exten ...