GeeksforGeeks - Remove all nodes which don’t lie in any path with sum>= k
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.
https://www.ideserve.co.in/learn/remove-all-nodes-which-lie-on-path-having-sum-less-than-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.
The idea is to traverse the tree and delete nodes in post-order traverse: 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.
Time Complexity: O(n), the solution does a single traversal of given Binary Tree.
/* 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);
}
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.
https://www.ideserve.co.in/learn/remove-all-nodes-which-lie-on-path-having-sum-less-than-k
public void deleteKLessPath(int k) {
int sum[] =
new
int[1];
deleteKLessPath(
this
.root, sum, k);
if
(sum[0] < k)
root =
null
;
}
public Node deleteKLessPath(Node node, int[] sum, int k) {
if
(node ==
null
)
return
null
;
int[] ls =
new
int[1];
int[] rs =
new
int[1];
ls[0] = rs[0] = sum[0] + node.data;
node.left = deleteKLessPath(node.left, ls, k);
node.right = deleteKLessPath(node.right, rs, k);
sum[0] = ls[0] > rs[0] ? ls[0] : rs[0];
if
(sum[0] < k) {
node =
null
;
}
return
node;
}
http://codereview.stackexchange.com/questions/59592/remove-all-nodes-which-dont-lie-in-any-path-with-sum-k
http://dheeraj9823.blogspot.com/2013/12/remove-all-nodes-which-dont-lie-in-any.html
http://stackoverflow.com/questions/19306914/remove-all-nodes-in-a-binary-three-which-don-t-lie-in-any-path-with-sum-k
GeeksforGeeks - Remove all nodes which don’t lie in any path with sum>= k
public boolean recurse(TreeNode node, int sum, int value) {
if (sum >= value) return true;
if (node == null) return false;
boolean left = recurse (node.left, sum + node.item, value);
boolean right = recurse (node.right, sum + node.item, value);
if (!left) {
node.left = null;
}
if (!right) {
node.right = null;
}
return left || right;
}
Different Version: We may change the return type to class with sum, Node roothttp://dheeraj9823.blogspot.com/2013/12/remove-all-nodes-which-dont-lie-in-any.html
http://stackoverflow.com/questions/19306914/remove-all-nodes-in-a-binary-three-which-don-t-lie-in-any-path-with-sum-k
public int removeNodesUtil(Node node, int sum, int k)
{
if (node == null) return sum;
sum =sum + node.data;
if (node.left == null && node.right == null)
{
if (sum < k)
{
node = null;
}
return sum;
}
int leftSum = 0, rightSum = 0, maxSum = 0;
if (node.left != null) {
leftSum = removeNodesUtil(node.left, sum, k);
if (leftSum < k) {
node.left = null;
}
}
if (node.right != null) {
rightSum = removeNodesUtil(node.right, sum, k);
if (rightSum < k) {
node.right = null;
}
}
maxSum = Math.max(leftSum, rightSum);
if (maxSum < k) {
node = null;
}
return maxSum;
}
http://www.sanfoundry.com/cpp-program-remove-nodes-not-lie-path-sum/GeeksforGeeks - Remove all nodes which don’t lie in any path with sum>= k