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

  • SEARCH
  • Home
  • 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 7429591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:59:35+00:00 2026-05-29T08:59:35+00:00

using a binary search tree I need to add to a vector all int

  • 0

using a binary search tree I need to add to a vector all int elements of the heaviest path of the tree.
for example I have 20,7,6,9,11,21
the values that should be added to the vector would be 20 ,7,9,11
I have written the implementation of the calculation of the heaviest path, but I don’t know how to change it so the right elements will be added to the vector:

int Tree::maxBranch(Node* node){
    if(node==NULL)
        return 0;
    int leftSum=node->data+maxBranch(node->left);
    int rightSum=node->data+maxBranch(node->right);
    if(rightSum>leftSum){
        return rightSum;
    }
    return leftSum;     
}
  • 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-05-29T08:59:36+00:00Added an answer on May 29, 2026 at 8:59 am

    Your code as written does not keep track of the branches it’s followed at all – it only gets the sum.

    You should change the function to take an std::vector<int>& as an argument (note the &, for a reference type, since you need to effectively return two values).

    Call it with an empty vector.

    Before the line where you do the recursive call on node->left, you should save the original vector as, for instance, std::vector<int> vec_left(input_vector) and std::vector<int> vec_right(input_vector). Then pass the two copies down to the recursive calls.

    Now, in the code, just before return rightSum, you should have a vec_right.push_back(node->id); input_vector = vec_right;. Just before return leftSum, you should likewise have a vec_left.push_back(node->id); input_vector = vec_left;. In English, you keep the path that was made by the branch which had the highest sum, and discard the other one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario where I need to search from many binary files (using
i have implement binary search tree in c++ #include <iostream> #include <cstdlib> using namespace
I have to implement a binary search tree using C++ for one of assignments.
I want to store some values in a balanced binary search tree using C#.
Okay so I have a Binary Search Tree built using only C structs and
I'm using a library that manipulates a binary search tree. In this library is
Is there a method to build a balanced binary search tree? Example: 1 2
I have a binary search tree created in C. The problem is I can't
I am trying to create a custom Binary Search Tree, and I have everything
Since black red tree is a binary search tree I have decided to use

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.