从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. Android 自定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自定义View之旅,前面已经介绍过一个自定义View的基础的例 ...

  2. 跨域请求,关于后端session会话丢失的解决办法

    目前使用前后端分离的模式开发,后端提供跨域接口.前端jsonp调用,绑定数据,但是在该站点下有个人中心模块存在的情况下,服务端的session会话会被跨域请求覆盖改掉 大家都知道tomcat使用coo ...

  3. [转] thrift的使用介绍

    http://gemantic.iteye.com/blog/1199214 一.About  thrift   二.什么是thrift,怎么工作? 三.Thrift  IDL 四.Thrift   ...

  4. 判断直线与线段相交 POJ 3304 Segments

    题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...

  5. web02--jsp数据传递

    1.创建一个login.jsp登陆界面 <%@ page language="java" import="java.util.*" pageEncodin ...

  6. Java 小型学生管理系统心得

    这个学生管理系统相对来说比较简单,主要就是复习下java怎么连接数据库,然后你怎么来实现这个功能,我简单的说下思路吧. 首先你要构思好这个界面,他包括增删查改这些基本功能,然后你去分析这些功能都能怎么 ...

  7. html图像入门

    在HTML中,图像由<img>标签定义. <img>是空标签,意思是说,它只包含属性,并且没有闭合标签. 要在页面上显示图像,需要使用源属性src, src指的是"s ...

  8. java BigDecimal的操作

    今天给大家讲一下java中BigDecimal的操作.由于double,float的精度不够,因此在进行商业计算的时候要使用的BigDecimal.BigDecimal对象创建如下: BigDecim ...

  9. Javascript 常用函数【1】

    1:基础知识 1 创建脚本块 1: <script language=”JavaScript”> 2: JavaScript code goes here 3: </script&g ...

  10. 总结:如何获取同一个DIV里的多个不同子标签的值,并赋值给input?

    这个问题说起来简单,但对于新手来说,也着实卡了好久,并且我在网上搜了好久没能找到合适的答案, 于是去博问问了一下,得到许多大神们的帮助与回答,接下来我就总结一下能够实现这个效果的几种方法,既为了自己更 ...