Remove all nodes which don’t lie in any path with sum>= k

Given a binary tree, a complete path is defined as a path from root to a leaf. The sum of all nodes on that path is defined as the sum of that path. Given a number K, you have to remove (prune the tree) all nodes which don’t lie in any path with sum>=k.

Note: A node can be part of multiple paths. So we have to delete it only in case when all paths from it have sum less than K.

Consider the following Binary Tree
          1 
      /      \
     2        3
   /   \     /  \
  4     5   6    7
 / \    /       /
8   9  12      10
   / \           \
  13  14         11
      / 
     15 

For input k = 20, the tree should be changed to following
(Nodes with values 6 and 8 are deleted)
          1 
      /      \
     2        3
   /   \        \
  4     5        7
   \    /       /
    9  12      10
   / \           \
  13  14         11
      / 
     15 

For input k = 45, the tree should be changed to following.
      1 
    / 
   2   
  / 
 4  
  \   
   9    
    \   
     14 
     /
    15 


The idea is to traverse the tree and delete nodes in bottom up manner. While traversing the tree, recursively calculate the sum of nodes from root to leaf node of each path. For each visited node, checks the total calculated sum against given sum “k”. If sum is less than k, then free(delete) that node (leaf node) and return the sum back to the previous node. Since the path is from root to leaf and nodes are deleted in bottom up manner, a node is deleted only when all of its descendants are deleted. Therefore, when a node is deleted, it must be a leaf in the current Binary Tree.

Following is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <stdlib.h>
  
// A utility function to get maximum of two integers
int max(int l, int r) { return (l > r ? l : r); }
  
// A Binary Tree Node
struct Node
{
    int data;
    struct Node *left, *right;
};
  
// A utility function to create a new Binary Tree node with given data
struct Node* newNode(int data)
{
    struct Node* node = (struct Node*) malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}
  
// print the tree in LVR (Inorder traversal) way.
void print(struct Node *root)
{
    if (root != NULL)
    {
        print(root->left);
        printf("%d ",root->data);
        print(root->right);
    }
}
  
/* Main function which truncates the binary tree. */
struct Node *pruneUtil(struct Node *root, int k, int *sum)
{
    // Base Case
    if (root == NULL)  return NULL;
  
    // Initialize left and right sums as sum from root to
    // this node (including this node)
    int lsum = *sum + (root->data);
    int rsum = lsum;
  
    // Recursively prune left and right subtrees
    root->left = pruneUtil(root->left, k, &lsum);
    root->right = pruneUtil(root->right, k, &rsum);
  
    // Get the maximum of left and right sums
    *sum = max(lsum, rsum);
  
    // If maximum is smaller than k, then this node
    // must be deleted
    if (*sum < k)
    {
        free(root);
        root = NULL;
    }
  
    return root;
}
  
// A wrapper over pruneUtil()
struct Node *prune(struct Node *root, int k)
{
    int sum = 0;
    return pruneUtil(root, k, &sum);
}
  
// Driver program to test above function
int main()
{
    int k = 45;
    struct Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->left->left->left = newNode(8);
    root->left->left->right = newNode(9);
    root->left->right->left = newNode(12);
    root->right->right->left = newNode(10);
    root->right->right->left->right = newNode(11);
    root->left->left->right->left = newNode(13);
    root->left->left->right->right = newNode(14);
    root->left->left->right->right->left = newNode(15);
  
    printf("Tree before truncation\n");
    print(root);
  
    root = prune(root, k); // k is 45
  
    printf("\n\nTree after truncation\n");
    print(root);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*; 
class GFG
{
  
// A utility function to get
// maximum of two integers 
static int max(int l, int r) 
    return (l > r ? l : r);
  
// A Binary Tree Node 
static class Node 
    int data; 
    Node left, right; 
}; 
  
static class INT
{
    int v;
INT(int a)
{
    v = a;
}
}
  
// A utility function to create 
// a new Binary Tree node with
// given data 
static Node newNode(int data) 
    Node node = new Node(); 
    node.data = data; 
    node.left = node.right = null
    return node; 
  
// print the tree in LVR 
// (Inorder traversal) way. 
static void print(Node root) 
    if (root != null
    
        print(root.left); 
        System.out.print(root.data + " "); 
        print(root.right); 
    
  
// Main function which
// truncates the binary tree. 
static Node pruneUtil(Node root, int k, 
                      INT sum) 
    // Base Case 
    if (root == null) return null
  
    // Initialize left and right 
    // sums as sum from root to 
    // this node (including this node) 
    INT lsum = new INT(sum.v + (root.data)); 
    INT rsum = new INT(lsum.v); 
  
    // Recursively prune left 
    // and right subtrees 
    root.left = pruneUtil(root.left, k, lsum); 
    root.right = pruneUtil(root.right, k, rsum); 
  
    // Get the maximum of
    // left and right sums 
    sum.v = max(lsum.v, rsum.v); 
  
    // If maximum is smaller 
    // than k, then this node 
    // must be deleted 
    if (sum.v < k) 
    
  
        root = null
    
  
    return root; 
  
// A wrapper over pruneUtil() 
static Node prune(Node root, int k) 
    INT sum = new INT(0); 
    return pruneUtil(root, k, sum); 
  
// Driver Code
public static void main(String args[])
    int k = 45
    Node root = newNode(1); 
    root.left = newNode(2); 
    root.right = newNode(3); 
    root.left.left = newNode(4); 
    root.left.right = newNode(5); 
    root.right.left = newNode(6); 
    root.right.right = newNode(7); 
    root.left.left.left = newNode(8); 
    root.left.left.right = newNode(9); 
    root.left.right.left = newNode(12); 
    root.right.right.left = newNode(10); 
    root.right.right.left.right = newNode(11); 
    root.left.left.right.left = newNode(13); 
    root.left.left.right.right = newNode(14); 
    root.left.left.right.right.left = newNode(15); 
  
    System.out.println("Tree before truncation\n"); 
    print(root); 
  
    root = prune(root, k); // k is 45 
  
    System.out.println("\n\nTree after truncation\n"); 
    print(root); 
}
  
// This code is contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A class to create a new Binary Tree
# node with given data 
class newNode:
    def __init__(self, data): 
        self.data = data 
        self.left = self.right = None
  
# prthe tree in LVR (Inorder traversal) way. 
def Print(root):
    if (root != None):
        Print(root.left) 
        print(root.data, end = " "
        Print(root.right)
  
# Main function which truncates
# the binary tree. 
def pruneUtil(root, k, Sum):
      
    # Base Case 
    if (root == None):
        return None
  
    # Initialize left and right Sums as 
    # Sum from root to this node 
    # (including this node) 
    lSum = [Sum[0] + (root.data)] 
    rSum = [lSum[0]] 
  
    # Recursively prune left and right 
    # subtrees 
    root.left = pruneUtil(root.left, k, lSum) 
    root.right = pruneUtil(root.right, k, rSum) 
  
    # Get the maximum of left and right Sums 
    Sum[0] = max(lSum[0], rSum[0]) 
  
    # If maximum is smaller than k, 
    # then this node must be deleted 
    if (Sum[0] < k[0]):
        root = None
    return root
  
# A wrapper over pruneUtil() 
def prune(root, k):
    Sum = [0
    return pruneUtil(root, k, Sum)
  
# Driver Code
if __name__ == '__main__':
    k = [45]
    root = newNode(1
    root.left = newNode(2
    root.right = newNode(3
    root.left.left = newNode(4
    root.left.right = newNode(5
    root.right.left = newNode(6
    root.right.right = newNode(7
    root.left.left.left = newNode(8
    root.left.left.right = newNode(9
    root.left.right.left = newNode(12
    root.right.right.left = newNode(10
    root.right.right.left.right = newNode(11
    root.left.left.right.left = newNode(13
    root.left.left.right.right = newNode(14
    root.left.left.right.right.left = newNode(15
  
    print("Tree before truncation")
    Print(root) 
    print()
    root = prune(root, k) # k is 45 
  
    print("Tree after truncation")
    Print(root)
  
# This code is contributed by PranchalK

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
  
// C# program to implement 
// the above approach 
public class GFG
{
  
// A utility function to get 
// maximum of two integers  
public static int max(int l, int r)
{
    return (l > r ? l : r);
}
  
// A Binary Tree Node  
public class Node
{
    public int data;
    public Node left, right;
}
  
public class INT
{
    public int v;
public INT(int a)
{
    v = a;
}
}
  
// A utility function to create  
// a new Binary Tree node with 
// given data  
public static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.left = node.right = null;
    return node;
}
  
// print the tree in LVR  
// (Inorder traversal) way.  
public static void print(Node root)
{
    if (root != null)
    {
        print(root.left);
        Console.Write(root.data + " ");
        print(root.right);
    }
}
  
// Main function which 
// truncates the binary tree.  
public static Node pruneUtil(Node root, int k, INT sum)
{
    // Base Case  
    if (root == null)
    {
        return null;
    }
  
    // Initialize left and right  
    // sums as sum from root to  
    // this node (including this node)  
    INT lsum = new INT(sum.v + (root.data));
    INT rsum = new INT(lsum.v);
  
    // Recursively prune left  
    // and right subtrees  
    root.left = pruneUtil(root.left, k, lsum);
    root.right = pruneUtil(root.right, k, rsum);
  
    // Get the maximum of 
    // left and right sums  
    sum.v = max(lsum.v, rsum.v);
  
    // If maximum is smaller  
    // than k, then this node  
    // must be deleted  
    if (sum.v < k)
    {
  
        root = null;
    }
  
    return root;
}
  
// A wrapper over pruneUtil()  
public static Node prune(Node root, int k)
{
    INT sum = new INT(0);
    return pruneUtil(root, k, sum);
}
  
// Driver Code 
public static void Main(string[] args)
{
    int k = 45;
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(4);
    root.left.right = newNode(5);
    root.right.left = newNode(6);
    root.right.right = newNode(7);
    root.left.left.left = newNode(8);
    root.left.left.right = newNode(9);
    root.left.right.left = newNode(12);
    root.right.right.left = newNode(10);
    root.right.right.left.right = newNode(11);
    root.left.left.right.left = newNode(13);
    root.left.left.right.right = newNode(14);
    root.left.left.right.right.left = newNode(15);
  
    Console.WriteLine("Tree before truncation\n");
    print(root);
  
    root = prune(root, k); // k is 45
  
    Console.WriteLine("\n\nTree after truncation\n");
    print(root);
}
}
  
  // This code is contributed by Shrikant13

chevron_right



Output:

Tree before truncation
8 4 13 9 15 14 2 12 5 1 6 3 10 11 7

Tree after truncation
4 9 15 14 2 1

Time Complexity: O(n), the solution does a single traversal of given Binary Tree.

A Simpler Solution:
The above code can be simplified using the fact that nodes are deleted in bottom up manner. The idea is to keep reducing the sum when traversing down. When we reach a leaf and sum is greater than the leaf’s data, then we delete the leaf. Note that deleting nodes may convert a non-leaf node to a leaf node and if the data for the converted leaf node is less than the current sum, then the converted leaf should also be deleted.

Thanks to vicky for suggesting this solution in below comments.

C++

#include
using namespace std;

// A Binary Tree Node
struct Node
{
int data;
struct Node *left, *right;
};

// A utility function to create a new Binary
// Tree node with given data
struct Node* newNode(int data)
{
struct Node* node =
(struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}

// print the tree in LVR (Inorder traversal) way.
void print(struct Node *root)
{
if (root != NULL)
{
print(root->left);
cout << root->data << " "; print(root->right);
}
}

/* Main function which truncates the binary tree. */
struct Node *prune(struct Node *root, int sum)
{
// Base Case
if (root == NULL) return NULL;

// Recur for left and right subtrees
root->left = prune(root->left, sum – root->data);
root->right = prune(root->right, sum – root->data);

// If we reach leaf whose data is smaller than sum,
// we delete the leaf. An important thing to note
// is a non-leaf node can become leaf when its
// chilren are deleted.
if (root->left==NULL && root->right==NULL)
{
if (root->data < sum) { free(root); return NULL; } } return root; } // Driver program to test above function int main() { int k = 45; struct Node *root = newNode(1); root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(12);
root->right->right->left = newNode(10);
root->right->right->left->right = newNode(11);
root->left->left->right->left = newNode(13);
root->left->left->right->right = newNode(14);
root->left->left->right->right->left = newNode(15);

cout << "Tree before truncation\n"; print(root); root = prune(root, k); // k is 45 cout << "\n\nTree after truncation\n"; print(root); return 0; } // This code is contributed // by Akanksha Rai [tabby title="C"]

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
#include <stdlib.h>
  
// A Binary Tree Node
struct Node
{
    int data;
    struct Node *left, *right;
};
  
// A utility function to create a new Binary
// Tree node with given data
struct Node* newNode(int data)
{
    struct Node* node =
       (struct Node*) malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}
  
// print the tree in LVR (Inorder traversal) way.
void print(struct Node *root)
{
    if (root != NULL)
    {
        print(root->left);
        printf("%d ",root->data);
        print(root->right);
    }
}
  
/* Main function which truncates the binary tree. */
struct Node *prune(struct Node *root, int sum)
{
    // Base Case
    if (root == NULL) return NULL;
  
    // Recur for left and right subtrees
    root->left = prune(root->left, sum - root->data);
    root->right = prune(root->right, sum - root->data);
  
    // If we reach leaf whose data is smaller than sum,
    // we delete the leaf.  An important thing to note
    // is a non-leaf node can become leaf when its
    // chilren are deleted.
    if (root->left==NULL && root->right==NULL)
    {
        if (root->data < sum)
        {
            free(root);
            return NULL;
        }
    }
  
    return root;
}
  
// Driver program to test above function
int main()
{
    int k = 45;
    struct Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->left->left->left = newNode(8);
    root->left->left->right = newNode(9);
    root->left->right->left = newNode(12);
    root->right->right->left = newNode(10);
    root->right->right->left->right = newNode(11);
    root->left->left->right->left = newNode(13);
    root->left->left->right->right = newNode(14);
    root->left->left->right->right->left = newNode(15);
  
    printf("Tree before truncation\n");
    print(root);
  
    root = prune(root, k); // k is 45
  
    printf("\n\nTree after truncation\n");
    print(root);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove all nodes which donot 
// lie on path having sum>= k
  
// Class representing binary tree node
class Node {
    int data;
    Node left;
    Node right;
  
    // Constructor to create a new node
    public Node(int data) {
        this.data = data;
        left = null;
        right = null;
    }
}
  
// class to truncate binary tree
class BinaryTree {
    Node root;
  
    // recursive method to truncate binary tree
    public Node prune(Node root, int sum) {
  
        // base case
        if (root == null)
            return null;
  
        // recur for left and right subtree
        root.left = prune(root.left, sum - root.data);
        root.right = prune(root.right, sum - root.data);
  
        // if node is a leaf node whose data is smaller
        // than the sum we delete the leaf.An important
        // thing to note is a non-leaf node can become
        // leaf when its children are deleted.
        if (isLeaf(root)) {
            if (sum > root.data)
                root = null;
        }
  
        return root;
    
  
    // utility method to check if node is leaf
    public boolean isLeaf(Node root) {
        if (root == null)
            return false;
        if (root.left == null && root.right == null)
            return true;
        return false;
    }
  
    // for print traversal
    public void print(Node root) {
  
        // base case
        if (root == null)
            return;
  
        print(root.left);
        System.out.print(root.data + " ");
        print(root.right);
    }
}
  
// Driver class to test above function
public class GFG {
    public static void main(String args[]) {
  
        BinaryTree tree = new BinaryTree();
  
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
        tree.root.right.left = new Node(6);
        tree.root.right.right = new Node(7);
        tree.root.left.left.left = new Node(8);
        tree.root.left.left.right = new Node(9);
        tree.root.left.right.left = new Node(12);
        tree.root.right.right.left = new Node(10);
        tree.root.right.right.left.right = new Node(11);
        tree.root.left.left.right.left = new Node(13);
        tree.root.left.left.right.right = new Node(14);
        tree.root.left.left.right.right.left = new Node(15);
  
        System.out.println("Tree before truncation");
        tree.print(tree.root);
  
        tree.prune(tree.root, 45);
  
        System.out.println("\nTree after truncation");
        tree.print(tree.root);
    }
}
  
// This code is contributed by Shweta Singh

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

"""
Python program to remove all nodes which don’t
lie in any path with sum>= k
"""
  
# binary tree node contains data field , left
# and right pointer
class Node:
      
    # constructor to create tree node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
  
# Function to remove all nodes which do not
# lie in th sum path
def prune(root, sum):
  
    # Base case
    if root is None:
        return None
  
    # Recur for left and right subtree
    root.left = prune(root.left, sum - root.data)
    root.right = prune(root.right, sum - root.data)
  
    # if node is leaf and sum is found greater
    # than data than remove node An important 
    # thing to remember is that a non-leaf node 
    # can become a leaf when its children are 
    # removed
    if root.left is None and root.right is None:
        if sum > root.data:
            return None
  
    return root
  
# inorder traversal
def inorder(root):
    if root is None:
        return
    inorder(root.left)
    print(root.data, "", end="")
    inorder(root.right)
  
# Driver program to test above function
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.left.left.left = Node(8)
root.left.left.right = Node(9)
root.left.right.left = Node(12)
root.right.right.left = Node(10)
root.right.right.left.right = Node(11)
root.left.left.right.left = Node(13)
root.left.left.right.right = Node(14)
root.left.left.right.right.left = Node(15)
  
print("Tree before truncation")
inorder(root)
prune(root, 45)
print("\nTree after truncation")
inorder(root)
  
# This code is contributed by Shweta Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
  
// C# program to remove all nodes which donot  
// lie on path having sum>= k 
  
// Class representing binary tree node 
public class Node
{
    public int data;
    public Node left;
    public Node right;
  
    // Constructor to create a new node 
    public Node(int data)
    {
        this.data = data;
        left = null;
        right = null;
    }
}
  
// class to truncate binary tree 
public class BinaryTree
{
    public Node root;
  
    // recursive method to truncate binary tree 
    public virtual Node prune(Node root, int sum)
    {
  
        // base case 
        if (root == null)
        {
            return null;
        }
  
        // recur for left and right subtree 
        root.left = prune(root.left, sum - root.data);
        root.right = prune(root.right, sum - root.data);
  
        // if node is a leaf node whose data is smaller 
        // than the sum we delete the leaf.An important 
        // thing to note is a non-leaf node can become 
        // leaf when its children are deleted. 
        if (isLeaf(root))
        {
            if (sum > root.data)
            {
                root = null;
            }
        }
  
        return root;
    }
  
    // utility method to check if node is leaf 
    public virtual bool isLeaf(Node root)
    {
        if (root == null)
        {
            return false;
        }
        if (root.left == null && root.right == null)
        {
            return true;
        }
        return false;
    }
  
    // for print traversal 
    public virtual void print(Node root)
    {
  
        // base case 
        if (root == null)
        {
            return;
        }
  
        print(root.left);
        Console.Write(root.data + " ");
        print(root.right);
    }
}
  
// Driver class to test above function 
public class GFG
{
    public static void Main(string[] args)
    {
  
        BinaryTree tree = new BinaryTree();
  
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
        tree.root.right.left = new Node(6);
        tree.root.right.right = new Node(7);
        tree.root.left.left.left = new Node(8);
        tree.root.left.left.right = new Node(9);
        tree.root.left.right.left = new Node(12);
        tree.root.right.right.left = new Node(10);
        tree.root.right.right.left.right = new Node(11);
        tree.root.left.left.right.left = new Node(13);
        tree.root.left.left.right.right = new Node(14);
        tree.root.left.left.right.right.left = new Node(15);
  
        Console.WriteLine("Tree before truncation");
        tree.print(tree.root);
  
        tree.prune(tree.root, 45);
  
        Console.WriteLine("\nTree after truncation");
        tree.print(tree.root);
    }
}
  
  // This code is contributed by Shrikant13

chevron_right



Output:

Tree before truncation
8 4 13 9 15 14 2 12 5 1 6 3 10 11 7

Tree after truncation
4 9 15 14 2 1

This article is contributed by Chandra Prakash. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


10


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.