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 8298551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:49:09+00:00 2026-06-08T15:49:09+00:00

This seems like it should be really easy but I’ve been having trouble with

  • 0

This seems like it should be really easy but I’ve been having trouble with this for quite some time. As the title says, I’m just trying to find the node in a Binary tree (not a BST!) with the smallest value and return it. I can write a recursive void function pretty easily that can at least assign the smallest value in the function, but I’m getting stuck on how to back track to previous nodes once I reach a NULL pointer.

I have a node class that has a pointer to a left and right child, each with its own value. Here is my (failed) attempt so far:

int preOrder(Node *node, int value, int count, int sizeOfTree)
{
  count++; //keeps track of whether or not we have traversed the whole tree

  if(value < node->getValue())
    value = node->getValue(); 

  if(count == sizeOfTree);
    return value;

  if(node == NULL)
    //Want to return to the previous function call
    //How do I do this for a non void function? 
    //for a void function, you could jsut type "return;" and the function
    //back tracks to your previous place in the tree
    //but since I'm returning a value, How would I go about doing this?

  //these 2 calls are incorrect but the idea is that I first traverse the left subtree
  //followed by a traversal of the right subtree.
  preOrder(node->getLeft(), value);

  preOrder(node->getRight(), value);

}

If possible, I would like to try and do this without keeping track of a “count” as well to make the code cleaner.
Let me know if anymore clarification is needed.

  • 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-08T15:49:10+00:00Added an answer on June 8, 2026 at 3:49 pm

    I don’t really understand why, in your original code, you need to keep track of the amount of elements traversed. Here is my solution:

    int find_min(Node* node)
    {
      int value = node->getValue()
    
      Node* left_node = node->getLeft();
      if (left_node != NULL)
      {
        int left_value = find_min(left_node);
        if (left_value < value)
          value = left_value;
      }
    
      Node* right_node = node->getRight();
      if (right_node != NULL)
      {
        int right_value = find_min(right_node);
        if (right_value < value)
          value = right_value;
      }
    
      return value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be really easy but I couln't find an answer
This seems like it should be easy, but I can't quite find an explanation
This seems like it should be something very easy to do, but every time
This seems like it should be easy, but Its not really working for me.
This seems like it should be really simple, but I'm unable to figure this
This seems like it should be an easy thing to do, but for the
It seems like this should be easy, but I'm no expert and google isn't
This should be easy but I'm having a hard time with it. Basically, I
This seems like it should be obvious but I can't figure it out. Suppose
This seems like it should be very easy...anyway, it is in MS SQL Server

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.