How to delete a node in BST?

Step 1:

Use the key to find this node in BST, time complexity is log(n)

Step 2:

after finding this node, we have 3 different conditions:

1, if this node‘s left and right children are null, just set this node‘s parent‘s link to this to null;

2, if this node only has one child, just replace this node with it‘s child;

3, if this node has two children, then we need to find the minimum key node in this node‘s right subtree, then replace this node with the minimum key node, then delete this minimum key node in the right subtree.

时间: 2024-10-09 09:03:28

How to delete a node in BST?的相关文章

Delete a node from BST

Delete a node from BST. 1 public class ZigzagIterator { 2 3 public void inorder(TreeNode root) { 4 if (root != null) { 5 inorder(root.left); 6 System.out.println(root.val + " "); 7 inorder(root.right); 8 } 9 } 10 11 public TreeNode delete(TreeNo

C++ delete a node from BST

首先, 从一个BST中删除一个节点可能很tricky. 例如, 对于如下一个BST of integers: 当我们想要删除某个节点的时候, 我们的第一步就是查找到这个元素值所在的位置. 接下来, 我们要保证删除这个节点之后, 我们的二叉搜索树依然是一个二叉搜索树(BST). 但是这个删除操作本身又是那么的complicated. 因为被删除的项又分为四种情况: (1): 被删除的节点是叶子节点, 即这个节点既没有左子树, 有没有右子树, 这是最简单的case. (2): 被删除的节点没有左子树

001 -- Circle LinkList, list initiate, add a new node, delete a node, and List traverse

#include <studio.h> #include <stdlib.h> typedef struct CLinkList { int data; struct CLinkList *next ; } node; /*initiate a circle list*/ void ds_init(node **pNode) { int item; node *temp; node *target; printf("please input the value of th

Find下一个Node in BST

给定一个node,找inorder traversal 里下一个. 最直接的想法是inorder遍历整个bst,然后在遍历的结果中查找.但是这样耗费多余的空间和时间. geeksforgeek (1)有parent指针 在cc150上有很好的讲解 三种情况: i 当前node的right != null, 则返回leftmost of right subtree ii 当前node.right == null 如果node为其parent的左孩子,则可以直接返回node.parent 如果nod

[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 node, then we can simply remove it case 2: if the delete node is has single side or substree case 3: it has two children, then to keep it as a valid bina

Cannot delete .... . Name node is in safe mode

如何关闭安全模式呢? bin/hadoop dfsadmin-safemode leave 原因: 在分布式文件系统启动的时候,开始的时候会有安全模式,当分布式文件系统处于安全模式的情况下,文件系统中的内容不允许修改也不允许删除,直到安全模式结束.安全模式主要是为了系统启动的时候检查各个DataNode上数据块的有效性,同时根据策略必要的复制或者删除部分数据块.运行期通过命令也可以进入安全模式.在实践过程中,系统启动的时候去修改和删除文件也会有安全模式不允许修改的出错提示,只需要等待一会儿即可.

450. Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

Leetcode: Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

450. Delete Node in a BST 删除bst中的一个节点

[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If