Remove Node in Binary Search Tree

Source: Internet
Author: User

Question:

Given a root of Binary Search Tree with the unique value for each node. Remove the node with given value. If There is no such a node with given value in the binary search tree, does nothing. You should keep the tree still a binary search tree after removal.

Example:

Given Binary search tree:

5

/    \

3 6

/    \

2 4

Remove 3, you can either return:

5

/    \

2 6

\

4

Or:

5

/    \

4 6

/

2

Analysis:

There are three situations to consider.

Case 1: When the target node is a leaf node, simply delete it.

Case 2: When the target node has only a left (right) subtree, the parent node of the target node and the left (right) subtree of the target node are connected.

Case 3: When the target node has left and right subtrees, first find the node with the smallest value in the tree (right sub-tree, the most left child node), then assign the minimum value to the target node, and finally delete the node with the minimum value.

This algorithm can be implemented clearly and concisely using recursion.

Code:

1  Public classSolution {2     /**3      * @paramroot:the root of the binary search tree.4      * @paramvalue:remove the node with given value.5      * @return: The root of the binary search tree after removal.6      */7      PublicTreeNode RemoveNode (TreeNode root,intvalue) {8         if(Root = =NULL) {9             return NULL;Ten         } One          A         if(Root.val >value) { -Root.left =RemoveNode (root.left, value); -}Else if(Root.val <value) { theRoot.right =RemoveNode (root.right, value); -}Else { -             //Case 1: Leaf node -             if(Root.left = =NULL&& Root.right = =NULL) { +Root =NULL; -             //Case 2: only one subtree +}Else if(Root.left = =NULL) { ARoot =Root.right; at}Else if(Root.right = =NULL) { -Root =Root.left; -             //Case 3: There are two sub-trees -}Else { -TreeNode temp = findmin (root.right);//find the minimum value of the right sub-tree -Root.val =Temp.val; inRoot.right =RemoveNode (Root.right, temp.val); -             } to         } +         returnRoot; -     } the      * TreeNode findmin (TreeNode root) { $          while(Root.left! =NULL) {Panax NotoginsengRoot =Root.left; -         } the         returnRoot; +     } A}

Complexity:

The time complexity is O (n), and N is the number of tree nodes.

Remove Node in Binary Search Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.