Given a binary tree, delete a node from it by making sure that tree shrinks from the bottom (i.e. the deleted node is replaced by bottom most and rightmost node). This different from BST deletion. Here we do not have any order among elements, so we replace with last element.
Delete 10 in below tree 10 / \ 20 30 Output : 30 / 20 Delete 20 in below tree 10 / \ 20 30 \ 40 Output : 10 / \ 40 30
1. Starting at root, find the deepest and rightmost node in binary tree and node which we want to delete.
2. Replace the deepest rightmost node’s data with node to be deleted.
3. Then delete the deepest rightmost node.
Inorder traversal before deletion : 7 11 12 10 15 9 8 Inorder traversal after deletion : 7 8 12 10 15 9
Note: We can also replace node’s data that is to be deleted with any node whose left and right child points to NULL but we only use deepest node in order to maintain the Balance of a binary tree.
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- AVL Tree | Set 2 (Deletion)
- Ternary Search Tree (Deletion)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Check whether a binary tree is a full binary tree or not
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a given binary tree is skewed binary tree or not?
- Binary Tree to Binary Search Tree Conversion