Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:25:56+00:00 2026-06-07T20:25:56+00:00

This is a problem in my research code and I am wondering what is

  • 0

This is a problem in my research code and I am wondering what is the efficient way to go about doing this. I am leaving out unnecessary details and I am presenting it in a manner so that the crux of the problem can be understood.

Lets say I have a binary tree with numbers on each node. I want to find the minimum sum of all the branches in the tree starting from root to leaf. Below is a very rough pseudocode.

int minimum(tree){
   // Some base cases
   left_min = minimum(tree->left);
   right_min= minimum(tree->right);
   if (left_min < right_min){
      return current_value + left_min;
   }
   else{
      return current_value + right_min;
   }
}

From this, I can compute the minimum. However, if I want to compute the nodes which give me the minimum, how would I go about? i.e. if the answer is say 14, I want to find out which nodes at each level in the tree add up to give me a sum of 14. What would be the efficient way to go about doing this with minimal change to the existing function? By minimal change, I mean I can add additional variables to keep track of the branch but not rewrite the function completely.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:25:58+00:00Added an answer on June 7, 2026 at 8:25 pm

    You can use a list or stack or queue instead of vector:

    typedef vector<yourIdType> idvec;
    
    int minimum(tree, idvec &path){
       // Some base cases
       idvec leftPath, rightPath;
       left_min = minimum(tree->left, leftPath);
       right_min= minimum(tree->right, rightPath);
       if (left_min < right_min){
          swap(path, leftPath);
          path.push_back(thisNodeId);
          return current_value + left_min;
       } else {
          swap(path, rightPath);
          path.push_back(thisNodeId);
          return current_value + right_min;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a homework problem that I have. I have been doing some research
According to my research whilst trying to solve this problem, it turns out that
This problem is not readily reproducible in a simple example here but was wondering
The more I research this problem, the more confused I seem to be getting.
This problem has bugged me so many times and i have now decided to
This problem is very strange and I'm hoping someone can help me. For the
This problem is happening for me on IE8 and Chrome which makes me think
This problem is baffling me: BEGIN; INSERT INTO sub_users(user_id, email) SELECT user_id FROM users
This problem pops around the network for years, I've found no good solution yet.
This problem is similar to when creating alternate colour table-rows, only we are dealing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.