Home > Mobile >  Assignment in Delete a node in Binary Search Tree
Assignment in Delete a node in Binary Search Tree

Time:07-12

Below is the code to delete a node in Binary Search Tree(BST) https://leetcode.com/submissions/detail/730887683/

I have a doubt .Why are we doing below i.e why are are assigning.

 root.left=deleteNode(root.left,key);

and why not

 deleteNode(root.left,key);

Similarly for root.right .

CodePudding user response:

Imagine a BST with 3 nodes, with the root node as 5 and the key as 3.

   5
 /   \
3     6

When the key is deleted from the (root)node, the (root)node.left value should become null, if it's not null then the tree remains just as it was originally.

Hence, it's important to assign the deleted "null" value to the root.left. root.left=deleteNode(root.left,key);

  • Related