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. subclipse安装后从svn资源库视图check out的资源无法创建server

    不要从根目录下check out,只要把根目录下所需要的项目check out即可.

  2. Linux环境PHP7.0安装

    原文地址:http://blog.csdn.net/21aspnet/article/details/47708763 PHP7和HHVM比较 PHP7的在真实场景的性能确实已经和HHVM相当, 在一 ...

  3. P1965 转圈游戏

    很容易可以得到,答案应该是(x+m*10^k)%n 很显然,用O(n)一定会卡爆,所以用快速幂来算,或者找一下循环节也是可以的. #include <bits/stdc++.h> usin ...

  4. HelloWorld之jetty运行

    jetty是一个轻便的嵌入式servlet容器.其启动运行非常简单.eclipse下运行jetty容器有如下几步, 一.建一个普通的java工程 二.把jetty需要的包导入工程分别是jetty-6. ...

  5. docker sonaqube

    postgresql:   image: orchardup/postgresql:latest   environment:   - POSTGRESQL_USER=sonar   - POSTGR ...

  6. Cloudera Hadoop什么是CDH及CDH版本介绍

    本文引用自:Cloudera Hadoop什么是CDH及CDH版本介绍http://www.aboutyun.com/thread-6788-1-1.html(出处: about云开发) 云技术新兴的 ...

  7. 安装Postman

    原文地址:http://blog.csdn.net/ouyang111222/article/details/45743831 ** (一)安装篇 ** Postman是一款功能强大的网页调试与发送网 ...

  8. ArcGIS API for Silverlight动态标绘的实现

    原文:ArcGIS API for Silverlight动态标绘的实现 1.下载2个dll文件,分别是: ArcGISPlotSilverlightAPI.dll 和 Matrix.dll 其下载地 ...

  9. 经典Bug 修改方法

    右击工程名,“显示包内容”,删除提示错误的所有相关内容,(想当初,仅仅是删了一个图片,就各种报错,clean也没用,删了重新运行,也不成功.....有能力的话,最好能FQ,google你会发现很多技巧 ...

  10. 【转】Android Paint之 setXfermode PorterDuffXfermode 讲解

    [置顶] Android Paint之 setXfermode PorterDuffXfermode 讲解 分类: android动效篇2015-04-07 17:23 978人阅读 评论(8) 收藏 ...