Given a Binary search tree, the task is to delete the leaf nodes from the binary search tree.
Example:
Input:
Output: 8 12 20 Explanation: Inorder before Deleting the leaf node 4 8 10 12 14 20 22 and Inorder after Deleting the leaf node 8 12 20
Approach:
The idea is to traverse given Binary Search Tree in inorder way. During traversal, At each node, we check if it is a leaf node, meaning it has no children. If so, we remove it. Otherwise, we proceed to its left and right children. By traversing through both subtrees, we ensure that all leaf nodes are deleted while keeping the internal nodes intact.
Below is the implementation of the above approach:
C++
// C++ Program to Remove all leaf nodes// from the binary search tree#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=nullptr;right=nullptr;}};// Function for inorder traversal in a BST.voidinorder(Node*root){if(root!=nullptr){inorder(root->left);cout<<root->data<<" ";inorder(root->right);}}// Delete leaf nodes from binary // search tree.Node*leafDelete(Node*root){if(root==nullptr)returnnullptr;if(root->left==nullptr&&root->right==nullptr){deleteroot;returnnullptr;}// Recursively delete in left and // right subtrees.root->left=leafDelete(root->left);root->right=leafDelete(root->right);returnroot;}intmain(){// Create a hard coded BST.// 20// / \ // 8 22// / \ // 4 12// / \ // 10 14Node*root=newNode(20);root->left=newNode(8);root->left->left=newNode(4);root->left->right=newNode(12);root->left->right->left=newNode(10);root->left->right->right=newNode(14);root->right=newNode(22);leafDelete(root);inorder(root);return0;}
C
// C Program to Remove all leaf nodes// from the binary search tree#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// Function for inorder traversal in a BSTvoidinorder(structNode*root){if(root!=NULL){inorder(root->left);printf("%d ",root->data);inorder(root->right);}}// Function to delete leaf nodes from // binary search treestructNode*leafDelete(structNode*root){if(root==NULL)returnNULL;if(root->left==NULL&&root->right==NULL){free(root);returnNULL;}// Recursively delete in left and right subtreesroot->left=leafDelete(root->left);root->right=leafDelete(root->right);returnroot;}structNode*createNode(intval){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=val;node->left=NULL;node->right=NULL;returnnode;}intmain(){// Create a hard-coded BST// 20// / \ // 8 22// / \ // 4 12// / \ // 10 14structNode*root=createNode(20);root->left=createNode(8);root->left->left=createNode(4);root->left->right=createNode(12);root->left->right->left=createNode(10);root->left->right->right=createNode(14);root->right=createNode(22);root=leafDelete(root);inorder(root);return0;}
Java
// Java Program to Remove all leaf nodes // from the binary search treeclassNode{intdata;Nodeleft,right;Node(intval){data=val;left=null;right=null;}}classGfG{// Function for inorder traversal in a BST.staticvoidinorder(Noderoot){if(root!=null){inorder(root.left);System.out.print(root.data+" ");inorder(root.right);}}// Delete leaf nodes from binary search tree.staticNodeleafDelete(Noderoot){if(root==null)returnnull;if(root.left==null&&root.right==null){root=null;returnnull;}// Recursively delete in left // and right subtrees.root.left=leafDelete(root.left);root.right=leafDelete(root.right);returnroot;}publicstaticvoidmain(String[]args){// Create a hard coded BST.// 20// / \// 8 22// / \// 4 12// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);root.right=newNode(22);leafDelete(root);inorder(root);}}
Python
# Python Program to Remove all leaf nodes# from the binary search treeclassNode:def__init__(self,val):self.data=valself.left=Noneself.right=None# Function for inorder traversal in a BST.definorder(root):ifrootisnotNone:inorder(root.left)print(root.data,end=" ")inorder(root.right)# Delete leaf nodes from binary search tree.defleafDelete(root):ifrootisNone:returnNoneifroot.leftisNoneandroot.rightisNone:delrootreturnNone# Recursively delete in left and right subtrees.root.left=leafDelete(root.left)root.right=leafDelete(root.right)returnrootif__name__=="__main__":# Create a hard coded BST.# 20# / \# 8 22# / \# 4 12# / \# 10 14root=Node(20)root.left=Node(8)root.left.left=Node(4)root.left.right=Node(12)root.left.right.left=Node(10)root.left.right.right=Node(14)root.right=Node(22)leafDelete(root)inorder(root)
C#
// C# Program to Remove all leaf nodes // from the binary search treeusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=null;right=null;}}classGfG{// Function for inorder traversal in a BST.staticvoidinorder(Noderoot){if(root!=null){inorder(root.left);Console.Write(root.data+" ");inorder(root.right);}}// Delete leaf nodes from binary search tree.staticNodeleafDelete(Noderoot){if(root==null)returnnull;if(root.left==null&&root.right==null){root=null;returnnull;}// Recursively delete in left and // right subtrees.root.left=leafDelete(root.left);root.right=leafDelete(root.right);returnroot;}staticvoidMain(string[]args){// Create a hard coded BST.// 20// / \// 8 22// / \// 4 12// / \// 10 14Noderoot=newNode(20);root.left=newNode(8);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);root.right=newNode(22);leafDelete(root);inorder(root);}}
JavaScript
// JavaScript Program to Remove all leaf nodes// from the binary search treeclassNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Function for inorder traversal in a BST.functioninorder(root){if(root!==null){inorder(root.left);console.log(root.data+" ");inorder(root.right);}}// Delete leaf nodes from binary search tree.functionleafDelete(root){if(root===null)returnnull;if(root.left===null&&root.right===null){root=null;returnnull;}// Recursively delete in left and // right subtrees.root.left=leafDelete(root.left);root.right=leafDelete(root.right);returnroot;}// Create a hard coded BST.// 20// / \// 8 22// / \// 4 12// / \// 10 14letroot=newNode(20);root.left=newNode(8);root.left.left=newNode(4);root.left.right=newNode(12);root.left.right.left=newNode(10);root.left.right.right=newNode(14);root.right=newNode(22);leafDelete(root);inorder(root);
Output
8 12 20
Time Complexity: O(n), where n is the number of nodes in the tree. Auxiliary Space: O(h), where h is the height of the tree.