LF.53.Delete In Binary Search Tree

Delete the target key K in the given binary search tree if the binary search tree contains K. Return the root of the binary search tree.

Find your own way to delete the node from the binary search tree, after deletion the binary search tree‘s property should be maintained.

Assumptions

There are no duplicate keys in the binary search tree

//==== this code is not right. needs to be updated

 1 public class Solution {
 2   public TreeNode delete(TreeNode root, int key) {
 3     // Write your solution here.
 4     if (root == null) {
 5         return null ;
 6     }
 7     //if found, then set the root to null(means the following leafs will be removed )
 8     if (root.key == key) {
 9         return null;
10     }
11     if (root.key < key) {
12         //go to the right
13         root.right = delete(root.right, key);
14     }
15     if (root.key > key) {
16         root.left = delete(root.left, key) ;
17     }
18     return root;
19   }
20 }

原文地址:https://www.cnblogs.com/davidnyc/p/8655271.html

时间: 2024-10-09 08:28:31

LF.53.Delete In Binary Search Tree的相关文章

LF.51.Insert In Binary Search Tree

Insert a key in a binary search tree if the binary search tree does not already contain the key. Return the root of the binary search tree. Assumptions There are no duplicate keys in the binary search tree If the key is already existed in the binary

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and

[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

LF.52.Search In Binary Search Tree

Find the target key K in the given binary search tree, return the node that contains the key if K is found, otherwise return null. Assumptions There are no duplicate keys in the binary search tree 1 public class Solution { 2 public TreeNode search(Tr

BST(Binary Search Tree)

原文链接:http://blog.csdn.net/jarily/article/details/8679280 1 /****************************************** 2 数据结构: 3 BST(Binary Search Tree),二叉查找树; 4 5 性质: 6 若结点的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 7 若结点的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 8 该结点的左.右子树也分别为二叉查找树; 9 10 遍

pat1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only

二叉查找树(binary search tree)

二叉查找树的性质:对于树中的每个节点x,它的左子树中所有项的值不大于x的值,它的右子树中所有项的值不小于x的值. 二叉查找树应具有以下操作 ① 寻找最小项 FIND_MIN ② 寻找最大项 FIND_MAX ③ 是否包含 CONTAINS ④ 树是否为空 IS_EMPTY ⑤ 清空树 MAKE_EMPTY ⑥ 插入节点 INSERT ⑦ 移除节点 REMOVE ⑧ 打印树 PRINT_TREE (中序遍历) ⑨ 深拷贝 DEEP_CLONE 二叉查找树(binary search tree)的抽

Binary Search Tree Iterator

QUESTION Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time

LeetCode99 Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. (Hard) Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 分析: BST的中序遍历应该是递增的,我们考