Remove all nodes which lie on a path having sum less than k
Last Updated : 23 Jul, 2025
Given a binary tree,a complete path is defined as a path from a root to a leaf. The sum of all nodes on that path is defined as the sum of that path. Given the number k, the task is to remove all nodes that lie on a path with a sum less than 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 a sum less than k.
Input: k= 20
Output:
Explanation: Nodes with values 8 and 6 are deleted because the path sum from root to node with value 8 is 15 and the path sum from root to node with value 6 is 10.
Approach:
The idea is to traverse the binary tree recursively. At each node, decrement the required sum of the path. At the end of paths, check if the sum is less than equal to 0. If it is yes, then return true (meaning this path has sum >= k). Else return false. For the nodes, if false is returned by left and right subtree, then delete the current node and return false. If false is returned by only one subtree, then set the corresponding pointer to null. Then return true.
Below is the implementation of the above approach:
C++
// C++ program to Remove all nodes which// don’t lie in any path with sum>= k#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};boolpruneTree(Node*root,intk){// If path has ended, check if it // satisfies the sum.if(root==nullptr)returnk<=0;// Check for left and right subtrees.boolleft=pruneTree(root->left,k-root->data);boolright=pruneTree(root->right,k-root->data);// If any path on left or right subtree does not // have required sum, then delete the current Node// and return false.if(left==false&&right==false){delete(root);returnfalse;}// Set left node as null, if it has been// deleted.elseif(left==false){root->left=nullptr;}// Set right node as null, if it has been// deleted.elseif(right==false){root->right=nullptr;}returntrue;}voidinOrder(Node*root){if(root==nullptr)return;inOrder(root->left);cout<<root->data<<" ";inOrder(root->right);}intmain(){// Binary tree// 1// / \ // 2 3// / \ \ // 3 5 2Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(3);root->left->right=newNode(5);root->right->right=newNode(2);intk=7;pruneTree(root,k);inOrder(root);return0;}
C
// C program to Remove all nodes which// don’t lie in any path with sum>= k#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left,*right;};// Function to prune the treeintpruneTree(structNode*root,intk){// If path has ended, check if it// satisfies the sum.if(root==NULL)returnk<=0;// Check for left and right subtrees.intleft=pruneTree(root->left,k-root->data);intright=pruneTree(root->right,k-root->data);// If any path on left or right subtree does not// have required sum, then delete the current Node// and return false.if(left==0&&right==0){free(root);return0;}// Set left node as null, if it has// been deleted.elseif(left==0){root->left=NULL;}// Set right node as null, if it has// been deleted.elseif(right==0){root->right=NULL;}return1;}voidinOrder(structNode*root){if(root==NULL)return;inOrder(root->left);printf("%d ",root->data);inOrder(root->right);}structNode*createNode(intx){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=x;node->left=NULL;node->right=NULL;returnnode;}intmain(){// Binary tree// 1// / \ // 2 3// / \ \ // 3 5 2structNode*root=createNode(1);root->left=createNode(2);root->right=createNode(3);root->left->left=createNode(3);root->left->right=createNode(5);root->right->right=createNode(2);intk=7;pruneTree(root,k);inOrder(root);return0;}
Java
// Java program to Remove all nodes which// don’t lie in any path with sum>= kimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Function to prune the treestaticbooleanpruneTree(Noderoot,intk){if(root==null)returnk<=0;booleanleft=pruneTree(root.left,k-root.data);booleanright=pruneTree(root.right,k-root.data);if(!left&&!right){returnfalse;}elseif(!left){root.left=null;}elseif(!right){root.right=null;}returntrue;}staticvoidinOrder(Noderoot){if(root==null)return;inOrder(root.left);System.out.print(root.data+" ");inOrder(root.right);}publicstaticvoidmain(String[]args){// Binary tree// 1// / \// 2 3// / \ \// 3 5 2Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(3);root.left.right=newNode(5);root.right.right=newNode(2);intk=7;pruneTree(root,k);inOrder(root);}}
Python
# Python program to Remove all nodes which# don’t lie in any path with sum>= kclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Function to prune the treedefpruneTree(root,k):# If path has ended, check if it# satisfies the sum.ifrootisNone:returnk<=0# Check for left and right subtrees.left=pruneTree(root.left,k-root.data)right=pruneTree(root.right,k-root.data)# If any path on left or right subtree does not# have required sum, then delete the current Node# and return False.ifnotleftandnotright:returnFalse# Set left node as None, if it has# been deleted.elifnotleft:root.left=None# Set right node as None, if it has # been deleted.elifnotright:root.right=NonereturnTruedefinOrder(root):ifrootisNone:returninOrder(root.left)print(root.data,end=" ")inOrder(root.right)if__name__=="__main__":# Binary tree# 1# / \# 2 3# / \ \# 3 5 2root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(3)root.left.right=Node(5)root.right.right=Node(2)k=7pruneTree(root,k)inOrder(root)
C#
// C# program to Remove all nodes which// don’t lie in any path with sum>= kusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Function to prune the treestaticboolpruneTree(Noderoot,intk){if(root==null)returnk<=0;boolleft=pruneTree(root.left,k-root.data);boolright=pruneTree(root.right,k-root.data);if(!left&&!right){returnfalse;}elseif(!left){root.left=null;}elseif(!right){root.right=null;}returntrue;}staticvoidinOrder(Noderoot){if(root==null)return;inOrder(root.left);Console.Write(root.data+" ");inOrder(root.right);}staticvoidMain(string[]args){// Binary tree// 1// / \// 2 3// / \ \// 3 5 2Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(3);root.left.right=newNode(5);root.right.right=newNode(2);intk=7;pruneTree(root,k);inOrder(root);}}
JavaScript
// JavaScript program to Remove all nodes which// don’t lie in any path with sum>= kclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Function to prune the treefunctionpruneTree(root,k){if(root===null)returnk<=0;letleft=pruneTree(root.left,k-root.data);letright=pruneTree(root.right,k-root.data);if(!left&&!right){returnfalse;}elseif(!left){root.left=null;}elseif(!right){root.right=null;}returntrue;}functioninOrder(root){if(root===null)return;inOrder(root.left);console.log(root.data);inOrder(root.right);}// Binary tree// 1// / \// 2 3// / \ \// 3 5 2letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(3);root.left.right=newNode(5);root.right.right=newNode(2);letk=7;pruneTree(root,k);inOrder(root);
Output
2 5 1
Time Complexity: O(n), where n is the number of nodes. Auxiliary Space: O(h),where h is the height of the tree.