Delete leaf nodes with value k

Given a binary tree and value k. delete all the leaf nodes with value equal to k. If a node becomes leaf after deletion, then it should be deleted if it has value k.

Examples:

Input :     4
         /     \
        5       5
       /  \    /
      3    1  5 

Output :    4
         /     
        5       
       /  \    
      3    1  



1. Use PostOrder traversal.
2. When we encounter leaf nodes, then we check whether it is leaf node or not.
3. If it is leaf node and value equal to k, then delete it.
4. Else, Recurse for other nodes.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to delete leaf nodes with
// value equal to k.
  
class Node {
    int data;
    Node left;
    Node right;
  
    public Node(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
  
public class LeafNodesWithValueK {
  
    // Function to delete leaf Node with value 
    // equal to k
    static Node delLeafValueK(Node root, int k)
    {
        if (root == null)
            return null;
  
        root.left = delLeafValueK(root.left, k);
        root.right = delLeafValueK(root.right, k);
  
        // If the node is leaf, and its 
        // value is equal to k
        if ((root.left == null &&
             root.right == null) && 
             root.data == k)
            return null;
  
        return root;
    }
  
    static void postOrder(Node root)
    {
       if (root == null)
          return;
       System.out.print(root.data + " ");
       postOrder(root.left); 
       postOrder(root.right);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        Node root = new Node(4);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(3);
        root.left.right = new Node(1);
        root.right.left = new Node(5);
  
        System.out.println("Nodes in postorder before deletion");
        postOrder(root);
        System.out.println();
        System.out.println("Nodes in post order after required deletion");
        int k = 5;
        delLeafValueK(root, k);
        postOrder(root);
        System.out.println();
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to delete leaf nodes with
// value equal to k.
using System;
  
public class Node 
{
    public int data;
    public Node left;
    public Node right;
  
    public Node(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
  
public class LeafNodesWithValueK 
{
  
    // Function to delete leaf Node with value 
    // equal to k
    static Node delLeafValueK(Node root, int k)
    {
        if (root == null)
            return null;
  
        root.left = delLeafValueK(root.left, k);
        root.right = delLeafValueK(root.right, k);
  
        // If the node is leaf, and its 
        // value is equal to k
        if ((root.left == null &&
            root.right == null) && 
            root.data == k)
            return null;
  
        return root;
    }
  
    static void postOrder(Node root)
    {
        if (root == null)
            return;
        Console.Write(root.data + " ");
        postOrder(root.left); 
        postOrder(root.right);
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        Node root = new Node(4);
        root.left = new Node(5);
        root.right = new Node(5);
        root.left.left = new Node(3);
        root.left.right = new Node(1);
        root.right.left = new Node(5);
  
        Console.WriteLine("Nodes in postorder before deletion");
        postOrder(root);
        Console.WriteLine();
        Console.WriteLine("Nodes in post order after required deletion");
        int k = 5;
        delLeafValueK(root, k);
        postOrder(root);
        Console.WriteLine();
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

Nodes in postorder before deletion
4 5 3 1 5 5 
Nodes in post order after required deletion
4 5 3 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar