Remove Node in Binary Search Tree 解答
从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 解答的更多相关文章
- 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. ...
- 【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 ...
- [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 ...
- 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 ...
- Validate Binary Search Tree 解答
Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d ...
- 数据结构基础---Binary Search Tree
/// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and sea ...
- Binary Search Tree Iterator 解答
Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized wi ...
- 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 ...
- 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 ...
随机推荐
- java GBK字符转换成为UTF-8编码字符
import java.util.HashMap; import java.util.Map; /** * 创建日期: 2014-04-18 10:36:25 * 作者: 黄飞 * mail:huan ...
- monkeyrunner 详细介绍
MonkeyRunner: monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以写出一个 ...
- android如何让service不被杀死
1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override public int onStartCom ...
- Java基础知识强化25:Java创建对象的四种方式
1. Java程序中对象的创建有四种方式: ● 调用new语句创建对象,最常见的一种 ● 运用反射手段创建对象,调用java.lang.Class 或者 java.lang.reflect.Const ...
- hdu 2156
#include <iostream> #include <stdio.h> using namespace std; int main() { int i,n; while( ...
- sql server根据日期或者月份查询聚合数据
/*****************************根据时间查询每天的数据***************************************/ @tm_start:开始时间 @tm ...
- DIV布局之道二:DIV块的嵌套,DIV盒子模型
本文讲解DIV块布局的第二种使用方式:嵌套.“DIV嵌套”在有些文献中也被称为“盒子模型”,说的通俗一点就是嵌套(一个大的DIV块内部又包含一个或多个DIV块). 请看如下代码: CSS部分: CSS ...
- PHP Socket编程起步
让我们以一个简单的例子开始---一个接收输入字符串,处理并返回这个字符串到客户端的TCP服务.下面是相应的代码: PHP 代码: ) or die("Could not read input ...
- 微信移动客户端内部浏览器分享到朋友圈,QQ空间代码
http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html <script src="http://re ...
- rest简单实例
http://www.cnblogs.com/fredric/archive/2012/03/03/2378680.html http://www.thinksaas.cn/topics/0/153/ ...