从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. 阅读underscore源码笔记

    本文为原创作品,可以转载,但请添加本文连接,谢谢传阅,本人博客已转移至github,地址为:jruif.github.io underscorejs,一个实用的的Javascript函数库,值得推荐, ...

  2. [Javascript] JavaScript Array Methods in Depth - push

    Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...

  3. [Redux] Reducer Composition with combineReducers()

    Previous, we do composition with objects: const todoApp = (state = {}, action) => { return { todo ...

  4. 总结了关于PHP xss 和 SQL 注入的问题(转)

    漏洞无非这么几类,XSS.sql注入.命令执行.上传漏洞.本地包含.远程包含.权限绕过.信息泄露.cookie伪造.CSRF(跨站请求)等.这些漏洞不仅仅是针对PHP语言的,本文只是简单介绍PHP如何 ...

  5. JAVA try-catch-finally-return

      正常执行流程: try执行,遇到异常就跳到catch执行(以使得程序不会崩溃): 不管有没有异常catch,最后都执行finally   含return语句执行流程分析: 若try块中return ...

  6. java 格式判断

    public class FormatChecker { /** * 判断是否含有汉字 * @param string */ public static boolean containChinese( ...

  7. querydsl的好处

    http://www.querydsl.com/ 封装了很多访问不同数据层平台的方法,提供统一的通用框架(统一的书写格式,以一种通用的API方式来构建查询).便于抽成统一数据层,昨晚底层,以后其他模块 ...

  8. memcache和activemq使用连接,然后close

    memcache和activemq使用连接,然后close

  9. ASP.Net页面间传值

    一.目前在ASP.NET中页面传值共有这么几种方式: 1.表单提交,   <form action= "target.aspx" method = "post&qu ...

  10. js实现数组内元素随机排序

    其实蛮容易实现的,关键是简洁与否,下面是我自己写的. function randomSort(a){ var arr = a, random = [], len = arr.length; for ( ...