GeeksforGeeks » Trees specific questions
Minimum/Maximum Sum path in A Binary Tree
(11 posts)-
Find and Print the Root to leaf path with minimum sum.Every node has a Integer value.No need to consider edge weight or no. of edges.Only 1 root-leaf path will be printed who has the minimum sum(i.e the sum of all nodes including the root and the leaf)
-
Algo:
1. Find the sum of the left most nodes of the tree.
2. If a node has only the right tree call the same function recursively and find the sum of the left most nodes of the subtree.If you do this, you will be able to find the minimum sum
-
@ikhanm : the above solution will work if it only a BST....what is it is a arbitrary Binary tree...then ur solution wont work
-
for a bst , no need to go so far...
just print the left most branch of the tree (definitely having minimum sum of nodes)
:) -
I think minimum sum we will if we move towards left where as max we will get using right subtree , we can get these using bst . any other type of tree is mentioned in the question?
-
Storing left sum and right sum for each node ... and check which is the minimum value ... simply print it ... works for binary trees i guess ... correct me if i m wrong ....
-
@ pearls-will your algo satisfy that the path selected is a patha from root to leaf??
-
// pass *minSum = 0xFFFFFFFF and sum = 0 to the first call // if all elements are positive then, we can change logic and pass minSum as -1. void findMinSum(node *root, int sum, unsigned int *minSum) { if (root == NULL) printf("Tree doesn't have any data\n"); if (root->left == NULL && root->right == NULL) { if (*minSum > sum + root->data) { *minSum = sum + root->data; } return; } if (root->left) { findMinSum(root->left, sum + root->data, minSum); } if (root->right) { findMinSum(root->right, sum + root->data, minSum); } } -
LL *path=NULL; // link list which will contain the path
initial call TreeMin(root,&path)int TreeMin(Tree *root,LL **p) { if(root==NULL) return 0; LL *lf=NULL, *rh=NULL; int l,r; l=TreeMin(root->left,&lf); r=TreeMin(root->right,&rh); LL Node; *p=Node; Node.val=root->value; if(l<r) { node.next=lf; return l+root->val; } else { node.next=rh; return r+root->val; } } -
@Jitendra Kumar, Your code does not compile. Even after I fix it, as shown below, it returns the incorrect return value. For example,
Tree Root = 4 Root->Left = 1 Root->Left->Left = NULL: Root->Left->Right = 3 Root->Right = 5;it returns 5 instead of 8. Thank you. Camster, Is it possible for you to fix your program?
int TreeMin(Tree *root,node **p) { if(root==NULL){ return 0; } node *lf=NULL, *rh=NULL; int l,r; l=TreeMin(root->left,&lf); r=TreeMin(root->right,&rh); node Node; Node.value = 0; Node.next = 0; *p=&Node; Node.value=root->value; if(l<r) { Node.next=lf; return l+root->value; } else { Node.next=rh; return r+root->value; } } -
maxSum=0; void findMinSum(root,depth,arr,sum) { if(root) { arr[depth]=root->data; findMinSum(root->left,depth+1,arr,sum+root->data); if(!(root->left) && !(root->right))//leaf found { if(sum>maxSum) { sum=maxSum; take a copy of arr[] to view the path after recursion is over. } } findMinSum(root->right,depth+1,arr,sum+root->data); } }
Reply
You must log in to post.