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 ...
随机推荐
- 《UNIX网络编程》之多客户连接服务端,可重用套接字对
该网络编程之客户端与服务端程序模板支持: 1. 多客户端同时连接服务端,即服务程序可以同时为多个客户端服务: 2. 服务端支持套接字对重用,即即使处于TIME_WAIT状态,仍可支持服务端重启: 3. ...
- SQL 使用Cursor(游标)遍历结果集
使用Cursor(游标)可以在存储过程中遍历select 结果集,对其进行相关的操作. Cursor(游标)语法格式 DECLARE 游标名称 CURSOR FOR SELECT 字段1,字段2,字段 ...
- int? 类型数据
在数据库操作中,会遇到在int的单元格恰好为NULL值的情况,这个时候我们可以直接判断是否为null然后进行赋值,有人就想那我刚好用一下:?表达式不就好了: ) ? ); 这时候编译器会报错,原因就是 ...
- Android Studio 安装
准备: JDK 7以及以上版本. Android Studio安装文件 中文站下载 http://www.android-studio.org/index.php/download exe ,包含S ...
- bzoj 3831 Little Bird (单调队列优化dp)
/*先贴个n*n的*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100 ...
- Nginx环境下常见的开源项目重写汇总
我们做PHP开发的,作者寒冰我觉得大部分时候都在跟开源的系统打交道.比如:Discuz.PHPCMS.ecshop.wordpress等开源系统.一般我们都是在本地搭建测试环境,用的web服务器都是a ...
- QT5-控件-QSpinBox和QDoubleSpinBox(用于通过控件调整整数和小数)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSpinBox> #in ...
- QML之窗口(无边框、透明及拖拽)
1.无边框 Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView. 无边框窗口代码如下: QQuickView viwer; //QQuickView继承 ...
- MySQL查询优化处理
查询的生命周期的下一步是将一个sql转化成一个执行计划,MySQL再依照这个执行计划和存储引擎进行交互.这包括多个子阶段:解析sql,预处理,优化sql执行计划.这个过程中任何错误(例如语法错误)都可 ...
- vim学习心得(一)——Cygwin下vim配置
关于Vi有很多传说.其中最为著名的是: “Vi是编辑器之神,Emacs是神的编辑器” Emacs没有用过,但是Vi在Linux经常使用,所以,掌握好vi非常重要!!! Vim(Vi Improved) ...