Python Binary Search Tree: Delete a node in a given Binary search tree (BST)
4. Delete Node in BST
Write a Python program to delete a node with the given key in a given binary search tree (BST).
Note: Search for a node to remove. If the node is found, delete the node.
Sample Solution:
Python Code:
Sample Output:
Original node: 5 3 2 4 7 6 None After deleting specified node: 5 3 2 7 6 None
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to delete a node with a given key from a BST and then perform an in-order traversal to verify the tree remains valid.
- Write a Python script to remove a node from a BST, handling all three cases (leaf, one child, two children), and then print the tree level-by-level.
- Write a Python program to implement node deletion in a BST and then measure the tree’s height before and after deletion to observe the impact on balance.
- Write a Python function to delete a node from a BST and then output both the deleted node’s value and the updated tree structure.
Go to:
Previous: Write a Python program to check whether a given a binary tree is a valid binary search tree (BST) or not.
Next: Write a Python program to convert a given array elements to a height balanced Binary Search Tree (BST).
Python Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.