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(TreeNode root, int val) {
12         if (root == null)
13             return root;
14
15         if (root.val > val) {
16             root.left = delete(root.left, val);
17         } else if (root.val < val) {
18             root.right = delete(root.right, val);
19         } else {
20             if (root.left == null && root.right == null) {
21                 return null;
22             } else if (root.left == null) {
23                 return root.right;
24             } else if (root.right == null) {
25                 return root.left;
26             } else {
27                 root.val = findMin(root.right).val;
28                 root.right = delete(root.right, root.val);
29             }
30         }
31         return root;
32     }
33
34     public TreeNode findMin(TreeNode n) {
35         if (n.left != null) {
36             return findMin(n.left);
37         }
38         return n;
39     }
40
41 }
42
43 class TreeNode {
44     TreeNode left;
45     TreeNode right;
46     int val;
47
48     public TreeNode(int i) {
49         val = i;
50     }
51 }
时间: 2024-12-09 23:41:39

Delete a node from BST的相关文章

C++ delete a node from BST

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

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 thi

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