GeeksforGeeks » Trees specific questions

Minimum/Maximum Sum path in A Binary Tree

(11 posts)
  1. Rishabh
    guest
    Posted 11 months ago #

    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)

  2. ikhanm
    Member
    Posted 11 months ago #

    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

  3. Anu
    guest
    Posted 11 months ago #

    @ikhanm : the above solution will work if it only a BST....what is it is a arbitrary Binary tree...then ur solution wont work

  4. mrn
    guest
    Posted 10 months ago #

    for a bst , no need to go so far...
    just print the left most branch of the tree (definitely having minimum sum of nodes)
    :)

  5. Nikhil
    guest
    Posted 10 months ago #

    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?

  6. pearls
    Member
    Posted 10 months ago #

    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 ....

  7. args
    guest
    Posted 10 months ago #

    @ pearls-will your algo satisfy that the path selected is a patha from root to leaf??

  8. nagaraju vemuri
    guest
    Posted 7 months ago #

    // 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);
       }
    }
  9. jitendra Kumar
    guest
    Posted 3 months ago #

    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;
                    }
        }
  10. camster
    Member
    Posted 3 months ago #

    @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;
                    }
    }
  11. Guddu Sharma
    Member
    Posted 1 month ago #

    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.

RSS feed for this topic