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:
- 5 -> 3 -> 9: Path Sum = 5 + 3 + 9 = 17.
- 5 -> 4 -> 9: Path Sum = 5 + 4 + 9 = 18.
- 5 -> 4 -> 8 -> 5 -> 2: Path Sum = 5 + 4 + 8 + 5 + 2 = 24.
Below is the tree after deleting the required nodes that such that sum of all paths is at least 27:
Input: K = 10
Output: 2 1 8 12 14 7 10
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. Below are the steps:
- 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 sum of all nodes till that node is at least K or not. If found to be true, remove that node by returning 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++14
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Tree Node Classstruct Node { int data; Node *left; Node *right; Node(int x) { data = x; left = right = NULL; }};// Utility function that deletes nodes// from the Tree such that every root// to leaf path sum is at least KNode *removePathLessThanK(Node *node, int K, int sum){ // Base Case if (node == NULL) { return NULL; } // Recurse to the left if (node->left != NULL) { node->left = removePathLessThanK( node->left, K, sum + node->left->data); } // Recurse to the right if (node->right != NULL) { node->right = removePathLessThanK( node->right, K, sum + node->right->data); } // Check path sum at leaf node // is lesser than K, return NULL if (node->left == NULL && node->right == NULL && sum < K) { node = NULL; return node; } // Otherwise return the // current node as it is return node;}// Function to print the preorder// traversal of the Treevoid viewTree(Node *node){ // If node is not NULL if (node != NULL) { // Print the node cout << node->data << " "; // Left and Right Traversal 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 Kvoid removePathLessThanKUtil(Node *node, int K, int sum){ // Function Call to delete Nodes Node *result = removePathLessThanK(node, K, sum); // Preorder Traversal of the // modified Tree viewTree(result);}// Driver Codeint main(){ // Given sum K int K = 27; // Given Binary Tree Node *root = NULL; root = new Node(5); root->right = new Node(3); root->left = new Node(4); root->left->left = new Node(9); root->right->right = new Node(9); root->left->right = new Node(8); root->left->right->right = new Node(11); root->left->right->left = new Node(5); root->left->right->left->left = new Node(6); root->left->right->left->right = new Node(2); root->right->right->right = new Node(4); // Function Call removePathLessThanKUtil(root, K, root->data);}// This code is contributed by mohit kumar 29 |
Java
// Java program for the above approachimport java.io.*;import java.util.*;// Tree Node Classclass Node { int data; Node left; Node right;}class path { // Function to insert node in the // given Binary Tree public Node insert(int val) { Node n = new Node(); n.data = val; n.left = null; n.right = null; return n; } // Utility function that deletes nodes // from the Tree such that every root // to leaf path sum is at least K public Node removePathLessThanK( Node node, int K, int sum) { // Base Case if (node == null) { return null; } // Recurse to the left if (node.left != null) { node.left = removePathLessThanK( node.left, K, sum + node.left.data); } // Recurse to the right if (node.right != null) { node.right = removePathLessThanK( node.right, K, sum + node.right.data); } // Check path sum at leaf node // is lesser than K, return NULL if (node.left == null && node.right == null && sum < K) { node = null; return node; } // Otherwise return the // current node as it is return node; } // Function to print the preorder // traversal of the Tree public void viewTree(Node node) { // If node is not NULL if (node != null) { // Print the node System.out.print(node.data + " "); // Left and Right Traversal 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 K public void removePathLessThanKUtil( Node node, int K, int sum) { // Function Call to delete Nodes Node result = removePathLessThanK( node, K, sum); // Preorder Traversal of the // modified Tree viewTree(result); }}// Driver Codeclass GFG { // Driver Code public static void main(String[] args) { // Given sum K int K = 27; // Object of class path path b = new path(); // Given Binary Tree Node root = null; root = b.insert(5); root.right = b.insert(3); root.left = b.insert(4); root.left.left = b.insert(9); root.right.right = b.insert(9); root.left.right = b.insert(8); root.left.right.right = b.insert(11); root.left.right.left = b.insert(5); root.left.right.left.left = b.insert(6); root.left.right.left.right = b.insert(2); root.right.right.right = b.insert(4); // Function Call b.removePathLessThanKUtil( root, K, root.data); }} |
5 4 8 5 6 11
Time Complexity: O(N), where N is the number of nodes in the given Tree.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count root to leaf paths having exactly K distinct nodes in a Binary Tree
- Print all the paths from root, with a specified sum in Binary tree
- Print all k-sum paths in a binary tree
- Count all k-sum paths in a Binary Tree
- Remove nodes on root to leaf paths of length < K
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Given a binary tree, how do you remove all the half nodes?
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Sum of lengths of all paths possible in a given tree
- Sum of all pair shortest paths in a Tree
- Print all root-to-leaf paths with maximum count of even nodes
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 : mohit kumar 29




