Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
Last Updated : 23 Jul, 2025
Given a Binary Tree and an integer k, the task is to delete nodes from the given Tree such that the sum of all nodes of all remaining root-to-leaf paths is at least k.
Examples:
Input: k = 27
Output: 5 4 8 5 6 11 Explanation: Below are the paths whose sum is less than 27:
Below is the tree after deleting the required nodes that such that sum of all paths is at least 27:
Approach:
The idea is to use recursion and perform the Postorder Traversal and delete those nodes whose addition to the path sum is less than k.
Follow the steps below to solve the problem:
Perform the Post Order Traversal on the given Tree and during this traversal pass the sum of all nodes from the root node to each node.
During traversal, if we reach the leaf node then check if the sum of all nodes till that node is less than k?. If found to be true, remove that node by returning the NULL node from that node.
Repeat the above step for every leaf node encounters in the update tree.
After the above steps, print the Preorder Traversal of the modified Tree.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=right=nullptr;}};// Utility function that deletes nodes// from the Tree such that every root// to leaf path sum is at least KNode*removePathLessThanK(Node*node,intk,intsum){// if node is null returnif(node==nullptr){returnnullptr;}// Recurse to the leftif(node->left!=nullptr){node->left=removePathLessThanK(node->left,k,sum+node->left->data);}// Recurse to the rightif(node->right!=nullptr){node->right=removePathLessThanK(node->right,k,sum+node->right->data);}// Check path sum at leaf node// is lesser than K, return NULLif(node->left==nullptr&&node->right==nullptr&&sum<k){node=nullptr;returnnode;}returnnode;}voidviewTree(Node*node){if(node!=nullptr){cout<<node->data<<" ";viewTree(node->left);viewTree(node->right);}}// Function that deletes the nodes// from the Tree such that every root// to leaf path sum is at least KvoidremovePathLessThanKUtil(Node*node,intk,intsum){// Function Call to delete NodesNode*result=removePathLessThanK(node,k,sum);// Preorder Traversal of the// modified TreeviewTree(result);}intmain(){intk=27;// Given Binary Tree// 5// / \ // 4 3// / \ \ // 9 8 9// / \ \ // 5 11 4// / \ // 6 2Node*root=nullptr;root=newNode(5);root->right=newNode(3);root->left=newNode(4);root->left->left=newNode(9);root->right->right=newNode(9);root->left->right=newNode(8);root->left->right->right=newNode(11);root->left->right->left=newNode(5);root->left->right->left->left=newNode(6);root->left->right->left->right=newNode(2);root->right->right->right=newNode(4);removePathLessThanKUtil(root,k,root->data);}
Java
// Java program for the above approachimportjava.util.*;classNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=right=null;}}classGfG{// Utility function that deletes nodes// from the Tree such that every root// to leaf path sum is at least kstaticNoderemovePathLessThanK(Nodenode,intK,intsum){// if node is null returnif(node==null){returnnull;}// Recurse to the leftif(node.left!=null){node.left=removePathLessThanK(node.left,K,sum+node.left.data);}// Recurse to the rightif(node.right!=null){node.right=removePathLessThanK(node.right,K,sum+node.right.data);}// Check path sum at leaf node// is lesser than K, return NULLif(node.left==null&&node.right==null&&sum<K){node=null;returnnode;}// Otherwise return the// current node as it isreturnnode;}// Function to print the preorder// traversal of the TreestaticvoidviewTree(Nodenode){// If node is not NULLif(node!=null){// Print the nodeSystem.out.print(node.data+" ");// Left and Right TraversalviewTree(node.left);viewTree(node.right);}}// Function that deletes the nodes// from the Tree such that every root// to leaf path sum is at least KstaticvoidremovePathLessThanKUtil(Nodenode,intK,intsum){// Function Call to delete NodesNoderesult=removePathLessThanK(node,K,sum);// Preorder Traversal of the// modified TreeviewTree(result);}publicstaticvoidmain(String[]args){intK=27;// Given Binary Tree// 5// / \// 4 3// / \ \// 9 8 9// / \ \// 5 11 4// / \// 6 2Noderoot=null;root=newNode(5);root.right=newNode(3);root.left=newNode(4);root.left.left=newNode(9);root.right.right=newNode(9);root.left.right=newNode(8);root.left.right.right=newNode(11);root.left.right.left=newNode(5);root.left.right.left.left=newNode(6);root.left.right.left.right=newNode(2);root.right.right.right=newNode(4);removePathLessThanKUtil(root,K,root.data);}}
Python
# Python program for the above approachclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Utility function that deletes nodes# from the Tree such that every root# to leaf path sum is at least KdefremovePathLessThanK(node,K,sum):# if node is null returnifnodeisNone:returnNone# Recurse to the leftifnode.leftisnotNone:node.left=removePathLessThanK \
(node.left,K,sum+node.left.data)# Recurse to the rightifnode.rightisnotNone:node.right=removePathLessThanK \
(node.right,K,sum+node.right.data)# Check path sum at leaf node# is lesser than K, return NULLifnode.leftisNoneandnode.right \
isNoneandsum<K:node=NonereturnnodereturnnodedefviewTree(node):# If node is not NULLifnodeisnotNone:# Print the nodeprint(node.data,end=" ")# Left and Right TraversalviewTree(node.left)viewTree(node.right)# Function that deletes the nodes# from the Tree such that every root# to leaf path sum is at least KdefremovePathLessThanKUtil(node,K,sum):# Function Call to delete Nodesresult=removePathLessThanK(node,K,sum)# Preorder Traversal of the# modified TreeviewTree(result)if__name__=="__main__":K=27# Given Binary Tree# 5# / \# 4 3# / \ \# 9 8 9# / \ \# 5 11 4# / \# 6 2root=Node(5)root.right=Node(3)root.left=Node(4)root.left.left=Node(9)root.right.right=Node(9)root.left.right=Node(8)root.left.right.right=Node(11)root.left.right.left=Node(5)root.left.right.left.left=Node(6)root.left.right.left.right=Node(2)root.right.right.right=Node(4)removePathLessThanKUtil(root,K,root.data)
C#
// C# program for the above approachusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// Utility function that deletes nodes// from the Tree such that every root// to leaf path sum is at least KstaticNoderemovePathLessThanK(Nodenode,intK,intsum){// if node is null returnif(node==null){returnnull;}// Recurse to the leftif(node.left!=null){node.left=removePathLessThanK(node.left,K,sum+node.left.data);}// Recurse to the rightif(node.right!=null){node.right=removePathLessThanK(node.right,K,sum+node.right.data);}// Check path sum at leaf node// is lesser than K, return NULLif(node.left==null&&node.right==null&&sum<K){node=null;returnnode;}returnnode;}staticvoidviewTree(Nodenode){// If node is not NULLif(node!=null){Console.Write(node.data+" ");viewTree(node.left);viewTree(node.right);}}// Function that deletes the nodes// from the Tree such that every root// to leaf path sum is at least KstaticvoidremovePathLessThanKUtil(Nodenode,intK,intsum){// Function Call to delete NodesNoderesult=removePathLessThanK(node,K,sum);// Preorder Traversal of the// modified TreeviewTree(result);}staticvoidMain(){intK=27;// Given Binary Tree// 5// / \// 4 3// / \ \// 9 8 9// / \ \// 5 11 4// / \// 6 2Noderoot=null;root=newNode(5);root.right=newNode(3);root.left=newNode(4);root.left.left=newNode(9);root.right.right=newNode(9);root.left.right=newNode(8);root.left.right.right=newNode(11);root.left.right.left=newNode(5);root.left.right.left.left=newNode(6);root.left.right.left.right=newNode(2);root.right.right.right=newNode(4);removePathLessThanKUtil(root,K,root.data);}}
JavaScript
// JavaScript program for the above approachclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Utility function that deletes nodes// from the Tree such that every root// to leaf path sum is at least KfunctionremovePathLessThanK(node,K,sum){// if node is null returnif(node===null){returnnull;}// Recurse to the leftif(node.left!==null){node.left=removePathLessThanK(node.left,K,sum+node.left.data);}// Recurse to the rightif(node.right!==null){node.right=removePathLessThanK(node.right,K,sum+node.right.data);}// Check path sum at leaf node// is lesser than K, return NULLif(node.left===null&&node.right===null&&sum<K){node=null;returnnode;}returnnode;}functionviewTree(node){// If node is not NULLif(node!==null){console.log(node.data+" ");viewTree(node.left);viewTree(node.right);}}// Function that deletes the nodes// from the Tree such that every root// to leaf path sum is at least KfunctionremovePathLessThanKUtil(node,K,sum){// Function Call to delete Nodesletresult=removePathLessThanK(node,K,sum);// Preorder Traversal of the// modified TreeviewTree(result);}letK=27;// Given Binary Tree// 5// / \// 4 3// / \ \// 9 8 9// / \ \// 5 11 4// / \// 6 2letroot=newNode(5);root.right=newNode(3);root.left=newNode(4);root.left.left=newNode(9);root.right.right=newNode(9);root.left.right=newNode(8);root.left.right.right=newNode(11);root.left.right.left=newNode(5);root.left.right.left.left=newNode(6);root.left.right.left.right=newNode(2);root.right.right.right=newNode(4);removePathLessThanKUtil(root,K,root.data);
Output
5 4 8 5 6 11
Time Complexity: O(n), where n is the number of nodes in the given Tree. Auxiliary Space: O(h)