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

Have you met this question in a real interview? Yes
Example
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 为root

1. if value < root.val, 要被删除的节点在左子树,往左子树递归,并把操作结束后的返回值作为新的root.left

2. if value > root.val, 要被删除的节点在右子树,往右子树递归, 并把操作结束后的返回值作为新的root.right

3. if root == null, 递归到了一个null点,说明要删的value不存在,return null,而这个null点的parent的相应子树本来也是null,对树的结构没有任何影响

4. if value == root.val,说明root是该被删除的了

  A. if root.left == null, return root.right

  B. if root.right == null, return root.left(这两个case其实包含了只有一个child和一个child都没有的三种情况)

  C. 如果两个children都存在,从右子树中找最小的node,与root交换,再递归调用函数在右子树中删除root.val

 /**
* 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
if (root == null) return null;
if (value < root.val)
root.left = removeNode(root.left, value);
else if (value > root.val)
root.right = removeNode(root.right, value);
else {
if (root.left == null) return root.right;
if (root.right == null) return root.left;
TreeNode minOfRight = findMin(root.right);
//swap root and minOfRight
int temp = root.val;
root.val = minOfRight.val;
minOfRight.val = temp;
root.right = removeNode(root.right, minOfRight.val);
}
return root;
} public TreeNode findMin(TreeNode cur) {
while (cur.left != null) {
cur = cur.left;
}
return cur;
}
}

Lintcode: Remove Node in Binary Search Tree的更多相关文章

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

  2. Remove Node in Binary Search Tree 解答

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

  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. 【Lintcode】095.Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  5. [Lintcode]Inorder Successor in Binary Search Tree(DFS)

    题意 略 分析 1.首先要了解到BST的中序遍历是递增序列 2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p-&g ...

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

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

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

  8. CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree

    Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...

  9. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. java语言中数值自动转换的优先顺序

    转换原则:从低精度向高精度转换byte .short.int.long.float.double.char数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中“悄然”进行的转换,不需要用 ...

  2. 泌尿系统 Excretory system

    https://zh.wikipedia.org/wiki/泌尿系统 泌尿系統,有時也歸類於排泄系統(Excretory system)的一部分,負責尿液的產生.運送.儲存與排泄.人類的泌尿系統包括左 ...

  3. PHP 依赖注入 (转)

    说这个话题之前先讲一个比较高端的思想--'依赖倒置原则' "依赖倒置是一种软件设计思想,在传统软件中,上层代码依赖于下层代码,当下层代码有所改动时,上层代码也要相应进行改动,因此维护成本较高 ...

  4. composer 272解决

    composer global require "fxp/composer-asset-plugin:~1.0.3"                                 ...

  5. 蓝牙的L2CAP协议

    1.概述     L2CAP能向上层提供面向连接的或者无连接的数据服务,拥有multiplexing capability and segmentation and reassembly operat ...

  6. php获得文件夹下所有文件的递归算法

    function my_scandir($dir){ $files=array(); if(is_dir($dir)) { if($handle=opendir($dir)) { while(($fi ...

  7. 用户控件UserControl图片资源定位(一)---Xaml引用图片

    MEF编程实现巧妙灵活松耦合组件化编程,一些细节需要花费不小心思去处理: 其中组件中若包含用户控件,且需要访问图片资源,那么Xaml引用资源需要做以下设置 1. 用户控件(usercontrol)所在 ...

  8. wpf前端设计

    http://www.cnblogs.com/w-wanglei/archive/2016/03/14/5274298.html#_nav_0

  9. 使用Dom的Range对象处理chrome和IE文本光标位置

    有这样一段js: var sel = obj.createTextRange(); sel.move('character', num); sel.collapse(); sel.select(); ...

  10. [LeetCode] Combination Sum (bfs)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...