Remove all nodes which lie on a path having sum less than k
Last Updated : 13 Jul, 2026
Given a binary tree and an integer k, prune the tree such that every root-to-leaf path in the resulting tree has a sum greater than or equal to k. Return the root of the pruned tree.
Note: A node may lie on multiple root-to-leaf paths. It should be removed only if all such paths through it have a sum less than k.
Examples:
Input: root[] = [1, 2, 3, 4, 5, N, 7, 8, 9, N, 12, 10, N, N, N, 13, 14, N, N, N, 11, N, 15], k = 20
Output: [1, 2, 3, 4, 5, N, 7, N, 9, N, 12, 10, N, 13, 14, N, 11, N, 15]
Explanation: Root-to-leaf paths with sum < 20 are removed. The path 1-3-6 (sum 10) and 1-2-4-8 (sum 15) are pruned, so nodes 6 and 8 are deleted.
Input: root[] = [1, 2, 3, 4, 5, N, 7, 8, 9, N, 12, 10, N, N, N, 13, 14, N, N, N, 11, N, 15], k = 33
Output: [1, 2, N, 4, N, 9, N, N, 14, 15, N]
Explanation: Only the path 1-2-4-9-14-15 has a sum of 45, which is the only path with sum >= 33. All other nodes are pruned since none of their paths reach a sum of 33 or more.
Iterative Post-order Pruning - O(n) Time and O(n) Space
Each node must know whether its children survive pruning before deciding its own fate, making this a post-order problem. To avoid recursion-depth issues on skewed trees, perform an iterative post-order traversal using two stacks. The first stack builds the traversal order, while the second yields nodes in post-order. Each entry stores the node, its parent, child direction, and remaining required sum. A node is pruned only if it is still a leaf when processed, ensuring no valid path is removed prematurely.
Illustration:
Take root = [1, 2, 3, 4, 5, N, 7, 8, 9, N, 12, 10, N, N, N, 13, 14, N, N, N, 11, N, 15], k = 20.
The first pass pushes every node onto stack s1, tracking the remaining sum needed at each node (k minus the sum of all ancestor values), and each popped node is pushed onto s2 - producing nodes in reverse pre-order, which is the same as post-order when read back.
Popping s2 processes nodes leaf-first: node 6 is a leaf with remaining sum 20 - 1 - 3 = 16 needed, but its own value 6 < 16, so it's pruned from its parent 3.
Node 8 is a leaf with remaining sum 20 - 1 - 2 - 4 = 13 needed, but its value 8 < 13, so it's pruned from its parent 4.
Node 4 is then re-examined: since its only child 8 was just pruned, it is once again a leaf. Its remaining sum needed is 20 - 1 - 2 = 17, and its value 4 < 17, so node 4 itself would also need checking against its remaining children - since node 9's subtree survives (paths through 9 reach sums well above 20), node 4 is not a leaf at this point and is kept.
This bottom-up resolution continues until the root is processed, producing the correctly pruned tree.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intval){data=val;left=right=nullptr;}};Node*pruneTree(Node*root,intk){if(root==nullptr)returnnullptr;// build post-order sequence: s1 drains into s2stack<tuple<Node*,Node*,bool,int>>s1;stack<tuple<Node*,Node*,bool,int>>s2;s1.push({root,nullptr,false,k});while(!s1.empty()){auto[node,parent,isLeft,remK]=s1.top();s1.pop();s2.push({node,parent,isLeft,remK});// subtract current node's value before passing k downif(node->left)s1.push({node->left,node,true,remK-node->data});if(node->right)s1.push({node->right,node,false,remK-node->data});}Node*newRoot=root;// process children before parentwhile(!s2.empty()){auto[node,parent,isLeft,remK]=s2.top();s2.pop();// still a leaf here means both children were already prunedif(node->left==nullptr&&node->right==nullptr&&node->data<remK){if(parent!=nullptr){if(isLeft)parent->left=nullptr;elseparent->right=nullptr;}else{newRoot=nullptr;}}}returnnewRoot;}stringprintTree(Node*root){vector<string>result;queue<Node*>q;q.push(root);while(!q.empty()){Node*curr=q.front();q.pop();if(curr==nullptr){result.push_back("N");}else{result.push_back(to_string(curr->data));q.push(curr->left);q.push(curr->right);}}while(!result.empty()&&result.back()=="N")result.pop_back();stringres="[";for(inti=0;i<(int)result.size();i++){res+=result[i];if(i!=(int)result.size()-1)res+=", ";}res+="]";returnres;}intmain(){Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->right=newNode(7);root->left->left->left=newNode(8);root->left->left->right=newNode(9);root->left->right->right=newNode(12);root->right->right->left=newNode(10);root->left->left->right->left=newNode(13);root->left->left->right->right=newNode(14);root->right->right->left->right=newNode(11);root->left->left->right->right->right=newNode(15);intk=20;Node*result=pruneTree(root,k);cout<<printTree(result)<<endl;return0;}