Remove all nodes which don’t lie in any path with sum>= k
http://algorithmsandme.in/2014/03/remove-all-nodes-in-binary-tree-which-dont-lie-in-path/
http://codingrecipies.blogspot.com/2014/07/trees-prune-sum-path.html
Bottom - up approach is being used to prune to the tree.
http://algorithmsandme.in/2014/03/remove-all-nodes-in-binary-tree-which-dont-lie-in-path/
This problem involves two subproblems, one is to find all paths of given sum and second is to remove nodes from tree.
Formally speaking, problem goes like : given a binary tree, defining a term “complete path sum” as, sum of values of nodes lying in a path from root to the leaf; now given a value ‘K’, find the k-heavy path and prune binary tree i.e. remove nodes which don’t lie on those paths.
For example, in below tree, if K is given as 65,resultant tree will look like: 30, 20 and 15
One way is to first get all nodes which are not part of any path and then one by one delete those nodes.
It requires two traversals of tree and as well as space to save all paths. If node can be deleted while calculating paths will save extra traversal ass well the space as path need not be stored anymore.
When is it confirmed that path we are following is not a path with given sum? At leaf node, right?
If a leaf node does not satisfy path condition, it can be deleted.
If a leaf node does not satisfy path condition, it can be deleted.
Now what happens to parent node of leaf node we just deleted? That cannot be deleted as there may be other subtrees of parent node which lead to a path with given sum. For each node, deletion is dependent on what comes from its subtrees.
Well, solution depends on solutions to subproblem, classic case for recursion? What would be base case? If leaf node is not part of path of given sum, delete it and return false indication to parent node. If it is part of path with given sum, return true indication to parent node.
At parent node, there are two possible conditions:
1. Both left subtree and right subtree return false
Parent has to be deleted as there is no possible path from this node. Also, return false indication to parent of current node.
2. Any one subtree returns false
Then current node cannot be deleted, but we have to appropriately set right or left subtree as NULL.
1. Both left subtree and right subtree return false
Parent has to be deleted as there is no possible path from this node. Also, return false indication to parent of current node.
2. Any one subtree returns false
Then current node cannot be deleted, but we have to appropriately set right or left subtree as NULL.
- int prunePath(Node *node, int sum ){
- if( !node ) return true;
- int subSum = sum - node->value;
- /* To check if left tree or right sub tree contributes to total sum */
- int leftVal = false, rightVal = false;
- /*Check if node is leaf node */
- int isLeaf = !( node->left || node->right );
- /* If node is leaf node and it is part of path with sum = given sum
- return true to parent node so tha parent node is not deleted */
- if(isLeaf && !subSum )
- return true;
- /* If node is leaf and it not part of path with sum equals to given sum
- Return false to parent node */
- else if(isLeaf && subSum ){
- return false;
- }
- /* If node is not leaf and there is left child Traverse to left subtree*/
- leftVal = prunePath(node->left, subSum);
- /* If node is not leaf and there is right child Traverse to right subtree*/
- rightVal = prunePath(node->right, subSum);
- /* This is crux of algo.
- 1. If both left sub tree and right sub tree cannot lead to path with
- given sum,Delete the node
- 2. If any one sub tree can lead to path with sum equal to given sum,
- do not delete the node */
- if(!(leftVal || rightVal) ){
- return false;
- }
- if(leftVal || rightVal ){
- if(leftVal)
- node->right = NULL;
- if(rightVal)
- node->left = NULL;
- return true;
- }
- return true ;
- }
- void inoderTraversal(Node * root){
- if(!root)
- return;
- inoderTraversal(root->left);
- inoderTraversal(root->right);
- }
- Node *createNode(int value){
- newNode->value = value;
- newNode->right= NULL;
- newNode->left = NULL;
- return newNode;
- }
- Node *addNode(Node *node, int value){
- if(node == NULL){
- return createNode(value);
- }
- else{
- if (node->value > value){
- node->left = addNode(node->left, value);
- }
- else{
- node->right = addNode(node->right, value);
- }
- }
- return node;
- }
http://codingrecipies.blogspot.com/2014/07/trees-prune-sum-path.html
Bottom - up approach is being used to prune to the tree.
public
static
boolean
prunePath(Node root,
int
givenSum ,
int
currSum)
{
if
(root==
null
) {
if
(givenSum==currSum)
return
true
;
else
return
false
;
}
boolean
leftFlag = prunePath(root.left , givenSum , currSum+root.data);
boolean
rightFlag = prunePath(root.right , givenSum , currSum+root.data);
if
(!leftFlag)
root.left=
null
;
if
(!rightFlag)
root.right=
null
;
return
(leftFlag || rightFlag ) ;//&&?
}