Remove all leaf nodes from the binary search tree Last Updated : 17 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a Binary search tree, the task is to delete the leaf nodes from the binary search tree. Example: Input: Output: 8 12 20Explanation: 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> 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); Output8 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. Remove all leaf nodes from the binary search tree Comment More infoAdvertise with us Next Article Insertion in Binary Search Tree (BST) D Dharmendra kumar Improve Article Tags : Misc Binary Search Tree DSA Practice Tags : Binary Search TreeMisc Similar Reads Binary Search Tree A Binary Search Tree (BST) is a type of binary tree data structure in which each node contains a unique key and satisfies a specific ordering property:All nodes in the left subtree of a node contain values strictly less than the nodeâs value. All nodes in the right subtree of a node contain values s 4 min read Introduction to Binary Search Tree Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the 3 min read Applications of BST Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward.The left subtree of a node contains only node 3 min read Applications, Advantages and Disadvantages of Binary Search Tree A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p 2 min read Insertion in Binary Search Tree (BST) Given a BST, the task is to insert a new node in this BST.Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is fo 15 min read Searching in Binary Search Tree (BST) Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp 7 min read Deletion in Binary Search Tree (BST) Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios:Case 1. Delete a Leaf Node in BST Case 2. Delete a Node with Single Child in BSTDeleting a single child node is also simple in BST. Copy the child to the node and delete the node. Case 3. Delete a Node w 10 min read Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1 10 min read Balance a Binary Search Tree Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height.Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height.Input: Output: Explanation: The above u 10 min read Self-Balancing Binary Search Trees Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree. The height is typically maintained in order of logN so that all operations take O(logN) time on average 4 min read Like