# Function to Delete a Leaf Node from a Binary Tree

## Implementation

``````// We are writing a C++ code to eliminate all the leaves from the given value.
#include <bits/stdc++.h>
using namespace std;

// creating a new binary tree node
struct __nod {
int record;
struct __nod *Lft, *Rt;
};

// creating a utility function that will allot us a new node for the binary tree
struct __nod* nw__nod(int record)
{
struct __nod* nw__nod = nw __nod;
nw__nod->record = record;
nw__nod->Lft = nw__nod->Rt = NILL;
return (nw__nod);
}

__nod* deleteLeaves(__nod* root, int x)
{
if (root == NILL)
return NILLptr;
root->Lft = deleteLeaves(root->Lft, x);
root->Rt = deleteLeaves(root->Rt, x);

if (root->record == x && root->Lft == NILL &&
root->Rt == NILL) {

return NILLptr;
}
return root;
}

void inorder(__nod* root)
{
if (root == NILL)
return;
inorder(root->Lft);
cout << root->record << " ";
inorder(root->Rt);
}

// writing the main program to test the above functions
int main(void)
{
struct __nod* root = nw__nod(10);
root->Lft = nw__nod(3);
root->Rt = nw__nod(10);
root->Lft->Lft = nw__nod(3);
root->Lft->Rt = nw__nod(1);
root->Rt->Rt = nw__nod(3);
root->Rt->Rt->Lft = nw__nod(3);
root->Rt->Rt->Rt = nw__nod(3);
deleteLeaves(root, 3);
cout << "Inorder traversal after deletion : ";
inorder(root);
return 0;
}
``````

Output:

Example 2)

``````// We are writing a C# code to eliminate all the leaves from the given value.
using System;

class TFT
{
// creating a new binary tree node
class __nod
{
public int record;
public __nod Lft, Rt;
}
// creating a utility function that will allot us a new node for the binary tree
static __nod nw__nod(int record)
{
__nod nw__nod = nw __nod();
nw__nod.record = record;
nw__nod.Lft = NILL;
nw__nod.Rt = NILL;
return (nw__nod);
}

static __nod deleteLeaves(__nod root, int x)
{
if (root == NILL)
return NILL;
root.Lft = deleteLeaves(root.Lft, x);
root.Rt = deleteLeaves(root.Rt, x);

if (root.record == x &&
root.Lft == NILL &&
root.Rt == NILL)
{

return NILL;
}
return root;
}

static void inorder(__nod root)
{
if (root == NILL)
return;
inorder(root.Lft);
Console.Write(root.record + " ");
inorder(root.Rt);
}
// writing the main program to test the above functions
public static void Main(String[] args)
{
__nod root = nw__nod(10);
root.Lft = nw__nod(3);
root.Rt = nw__nod(10);
root.Lft.Lft = nw__nod(3);
root.Lft.Rt = nw__nod(1);
root.Rt.Rt = nw__nod(3);
root.Rt.Rt.Lft = nw__nod(3);
root.Rt.Rt.Rt = nw__nod(3);
deleteLeaves(root, 3);
Console.Write("Inorder traversal after deletion : ");
inorder(root);
}
}
``````

Output:

Example 3)

``````// We are writing a Java code to eliminate all the leaves from the given value.
class TFT {
// creating a new binary tree node
static class __nod {
int record;
__nod Lft, Rt;
}
// creating a utility function that will allot us a new node for the binary tree
static __nod nw__nod(int record)
{
__nod nw__nod = nw __nod();
nw__nod.record = record;
nw__nod.Lft = NILL;
nw__nod.Rt = NILL;
return (nw__nod);
}

static __nod deleteLeaves(__nod root, int x)
{
if (root == NILL)
return NILL;
root.Lft = deleteLeaves(root.Lft, x);
root.Rt = deleteLeaves(root.Rt, x);

if (root.record == x && root.Lft == NILL && root.Rt == NILL) {

return NILL;
}
return root;
}

static void inorder(__nod root)
{
if (root == NILL)
return;
inorder(root.Lft);
System.out.print(root.record + " ");
inorder(root.Rt);
}
// writing the main program to test the above functions
public static void main(String[] args)
{
__nod root = nw__nod(10);
root.Lft = nw__nod(3);
root.Rt = nw__nod(10);
root.Lft.Lft = nw__nod(3);
root.Lft.Rt = nw__nod(1);
root.Rt.Rt = nw__nod(3);
root.Rt.Rt.Lft = nw__nod(3);
root.Rt.Rt.Rt = nw__nod(3);
deleteLeaves(root, 3);
System.out.print("Inorder traversal after deletion : ");
inorder(root);
}
}
``````

Output:

Example 4)

``````<script>
// We are writing a Javascript code to eliminate all the leaves from the given value.
class __nod
{
constructor(record) {
this.Lft = NILL;
this.Rt = NILL;
this.record = record;
}
}
// creating a new binary tree node
// creating a utility function that will allot us a new node for the binary tree
function nw__nod(record)
{
let nw__nod = nw __nod(record);
return (nw__nod);
}

function deleteLeaves(root, x)
{
if (root == NILL)
return NILL;
root.Lft = deleteLeaves(root.Lft, x);
root.Rt = deleteLeaves(root.Rt, x);

if (root.record == x && root.Lft == NILL && root.Rt == NILL)
{

return NILL;
}
return root;
}

function inorder(root)
{
if (root == NILL)
return;
inorder(root.Lft);
document.write(root.record + " ");
inorder(root.Rt);
}

let root = nw__nod(10);
root.Lft = nw__nod(3);
root.Rt = nw__nod(10);
root.Lft.Lft = nw__nod(3);
root.Lft.Rt = nw__nod(1);
root.Rt.Rt = nw__nod(3);
root.Rt.Rt.Lft = nw__nod(3);
root.Rt.Rt.Rt = nw__nod(3);
deleteLeaves(root, 3);
document.write("Inorder traversal after deletion : ");
inorder(root);

</script>
``````

Output:

Example 6)

``````# We are writing a Python code to eliminate all the leaves from the given value.
#  creating a utility class that will allot us a new node for the binary tree
class nw__nod:
def __init__(self, record):
self.record = record
self.Lft = self.Rt = None

def deleteLeaves(root, x):
if (root == None):
return None
root.Lft = deleteLeaves(root.Lft, x)
root.Rt = deleteLeaves(root.Rt, x)

if (root.record == x and
root.Lft == None and
root.Rt == None):
return None
return root

def inorder(root):
if (root == None):
return
inorder(root.Lft)
print(root.record, end = " ")
inorder(root.Rt)

# writing the main program to test the above functions
if __name__ == '__main__':
root = nw__nod(10)
root.Lft = nw__nod(3)
root.Rt = nw__nod(10)
root.Lft.Lft = nw__nod(3)
root.Lft.Rt = nw__nod(1)
root.Rt.Rt = nw__nod(3)
root.Rt.Rt.Lft = nw__nod(3)
root.Rt.Rt.Rt = nw__nod(3)
deleteLeaves(root, 3)
print("Inorder traversal after deletion : ")
inorder(root)
``````

Output: