VOOZH about

URL: https://www.geeksforgeeks.org/dsa/deletion-of-a-given-node-k-in-a-binary-tree-using-level-order-traversal/

⇱ Deletion of a given node K in a Binary Tree using Level Order Traversal - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deletion of a given node K in a Binary Tree using Level Order Traversal

Last Updated : 12 Jul, 2025

Given a binary tree and a node K, the task is to delete the node K from it by making sure that tree shrinks from the bottom (i.e. the deleted node is replaced by bottom-most and rightmost node) using Level Order Traversal.

Examples: 

Input: K = 8, Tree = 
 

👁 Image


Output: 
 

👁 Image


Explanation: 
Please Refer below for explanation.

Input: K = 1, Tree = 
 

👁 Image


Output: 
 

👁 Image

Approach: 

  • Start searching from the root, the address of node which is to be deleted by traversing in level order-wise.
  • Continue Traversing Tree to find the deepest and rightmost node in level order wise to find the deepest and the rightmost node.
  • If the node to delete is different from rightmost deepest node, then replace the node to be deleted with rightmost deepest node and delete the later node
  • If the node to delete is same as rightmost deepest node, then simply delete the node.

For example: Consider the Example 1 from above 
 

👁 Image


Below is the implementation of the above approach.


Output: 
Original Tree: 1 8 3 4 5 6 7 
Deleting node with key 8: 1 7 3 4 5 6 
Deleting node with key 1: 6 7 3 4 5 
Deleting node with key 4: 6 7 3 5

 


 

Comment