Open In App

Remove all leaf nodes from the binary search tree

Last Updated : 17 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Binary search tree, the task is to delete the leaf nodes from the binary search tree. 

Example: 

Input:

Remove-all-leaf-nodes-from-the-binary-search-tree-1

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

Remove-all-leaf-nodes-from-the-binary-search-tree-2

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>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int val) {
        data = val;
        left = nullptr;
      	right = nullptr;
    }
};


// Function for inorder traversal in a BST.
void inorder(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)
        return nullptr;
    if (root->left == nullptr && root->right == nullptr) {
        delete root;
        return nullptr;
    }

    // Recursively delete in left and 
  	// right subtrees.
    root->left = leafDelete(root->left);
    root->right = leafDelete(root->right);

    return root;
}

int main() {
  
    // Create a hard coded BST.
    //        20
    //       /  \
    //      8   22
    //     / \
    //   4   12
    //       /  \
    //     10   14
  
    Node* root = new Node(20);
    root->left = new Node(8);
    root->left->left = new Node(4);
    root->left->right = new Node(12);
    root->left->right->left = new Node(10);
    root->left->right->right = new Node(14);
    root->right = new Node(22);
  
    leafDelete(root);
    inorder(root);
    return 0;
}
C
// C Program to Remove all leaf nodes
// from the binary search tree
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};


// Function for inorder traversal in a BST
void inorder(struct Node* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

// Function to delete leaf nodes from 
// binary search tree
struct Node* leafDelete(struct Node* root) {
    if (root == NULL)
        return NULL;
    if (root->left == NULL && root->right == NULL) {
        free(root);
        return NULL;
    }

    // Recursively delete in left and right subtrees
    root->left = leafDelete(root->left);
    root->right = leafDelete(root->right);

    return root;
}

struct Node* createNode(int val) {
    struct Node* node = 
      	(struct Node*)malloc(sizeof(struct Node));
    node->data = val;
    node->left = NULL;
    node->right = NULL;
    return node;
}

int main() {
  
    // Create a hard-coded BST
    //        20
    //       /  \
    //      8   22
    //     / \
    //   4   12
    //       /  \
    //     10   14
  
    struct Node* 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);
    return 0;
}
Java
// Java Program to Remove all leaf nodes 
// from the binary search tree

class Node {
    int data;
    Node left, right;

    Node(int val) {
        data = val;
        left = null;
      	right = null;
    }
}

class GfG {
  
    // Function for inorder traversal in a BST.
    static void inorder(Node root) {
        if (root != null) {
            inorder(root.left);
            System.out.print(root.data + " ");
            inorder(root.right);
        }
    }

    // Delete leaf nodes from binary search tree.
    static Node leafDelete(Node root) {
        if (root == null)
            return null;
        if (root.left == null && root.right == null) {
            root = null;
            return null;
        }

        // Recursively delete in left 
      	// and right subtrees.
        root.left = leafDelete(root.left);
        root.right = leafDelete(root.right);

        return root;
    }

    public static void main(String[] args) {
      
       // Create a hard coded BST.
        //        20
        //       /  \
        //      8   22
        //     / \
        //   4   12
        //       /  \
        //     10   14
        Node root = new Node(20);
        root.left = new Node(8);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(14);
        root.right = new Node(22);

        leafDelete(root);
        inorder(root);
    }
}
Python
# Python Program to Remove all leaf nodes
# from the binary search tree

class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None

# Function for inorder traversal in a BST.
def inorder(root):
    if root is not None:
        inorder(root.left)
        print(root.data, end=" ")
        inorder(root.right)

# Delete leaf nodes from binary search tree.
def leafDelete(root):
    if root is None:
        return None
    if root.left is None and root.right is None:
        del root
        return None

    # Recursively delete in left and right subtrees.
    root.left = leafDelete(root.left)
    root.right = leafDelete(root.right)

    return root

if __name__ == "__main__":
  
    # Create a hard coded BST.
    #        20
    #       /  \
    #      8   22
    #     / \
    #   4   12
    #       /  \
    #     10   14
    root = 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 tree

using System;

class Node {
    public int data;
    public Node left, right;

    public Node(int val) {
        data = val;
        left =  null;
        right = null;
    }
}

class GfG {

    // Function for inorder traversal in a BST.
    static void inorder(Node root) {
      
        if (root != null) {
            inorder(root.left);
            Console.Write(root.data + " ");
            inorder(root.right);
        }
    }

    // Delete leaf nodes from binary search tree.
    static Node leafDelete(Node root) {
      
        if (root == null)
            return null;
        if (root.left == null && root.right == null) {
            root = null;
            return null;
        }

        // Recursively delete in left and 
      	// right subtrees.
        root.left = leafDelete(root.left);
        root.right = leafDelete(root.right);

        return root;
    }

    static void Main(string[] args) {
      
         // Create a hard coded BST.
        //        20
        //       /  \
        //      8   22
        //     / \
        //   4   12
        //       /  \
        //     10   14
        Node root = new Node(20);
        root.left = new Node(8);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(14);
        root.right = new Node(22);

        leafDelete(root);
        inorder(root);
    }
}
JavaScript
// JavaScript Program to Remove all leaf nodes
// from the binary search tree

class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

// Function for inorder traversal in a BST.
function inorder(root) {
    if (root !== null) {
        inorder(root.left);
        console.log(root.data + " ");
        inorder(root.right);
    }
}

// Delete leaf nodes from binary search tree.
function leafDelete(root) {
    if (root === null)
        return null;
    if (root.left === null && root.right === null) {
        root = null;
        return null;
    }

    // Recursively delete in left and 
    // right subtrees.
    root.left = leafDelete(root.left);
    root.right = leafDelete(root.right);

    return root;
}


// Create a hard coded BST.
//        20
//       /  \
//      8   22
//     / \
//   4   12
//       /  \
//     10   14
let root = new Node(20);
root.left = new Node(8);
root.left.left = new Node(4);
root.left.right = new Node(12);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);
root.right = new Node(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.


Next Article
Article Tags :
Practice Tags :

Similar Reads