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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:26:41+00:00 2026-05-31T22:26:41+00:00

So I have a homework question where I’m supposed to use a recursive method

  • 0

So I have a homework question where I’m supposed to use a recursive method to “find the minimum element within a subtree rooted at the specified node”

And then I’m given this as my starting point:

   public TreeNode
{
    int data;
    TreeNode left;
    TreeNode right;
}

and

/**
Finds the minimum value for the subtree that is 
rooted at a given node
@param n The root of the subtree
@return The minimum value 
PRECONDITION: n is not null.
*/
int min(TreeNode n)
{
  // COMPLETE THE BODY OF THIS METHOD
}

Now, I’ve got a very basic driver program written to insert nodes into the tree and I’ve written my recursive method, but it seems to be counting up instead of down, here’s my method:

int min(TreeNode n){      
if(n.left != null) {
  n = n.left;
  min(n);
  System.out.println("N is now " + n.value);
 }
    return n.value;
   }

Output of my code:

Building tree with rootvalue 25
  =================================
  Inserted 11 to left of node 25
  Inserted 15 to right of node 11
  Inserted 16 to right of node 15
  Inserted 23 to right of node 16
  Inserted 79 to right of node 25
  Inserted 5 to left of node 11
  Inserted 4 to left of node 5
  Inserted 2 to left of node 4
    Root is 25
    N is now 2
    N is now 4
    N is now 5
    N is now 11
    The minimum integer in the given nodes subtree is: 11

Can someone please explain to me why this doesn’t work?

  • 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-31T22:26:43+00:00Added an answer on May 31, 2026 at 10:26 pm

    Note: this is all assuming you’re in a Binary Search Tree, so returning the minimum element means returning the left-most element.

    This means your recursive call is quite simple:

    min(node):
      if this node has a left node:
        return min(node.left)
      if this node does not have a left node:
        return this node's value
    

    The logic is that if we don’t have another left node then we are the left-most node, so we are the minimum value.

    Now, in Java:

    int min(TreeNode n){
      if (n.left == null)
        return n.value;
      return min(n.left); // n.left cannot be null here
    }
    

    Now to explain your results, consider how this method works. It calls the method on the next node (min(n.left)) before continuing. In your case you had a println after this recursive call. Therefore the println inside the recursive call went first. So your prints started at the bottom of the tree and worked their way back up. This explains the “reverse order” printing.
    Your method then returned 11 as your result because (as another answer has explained) your n = n.left didn’t affect any of your recursive sub-calls, only the one in the current function call. This means you returned the left node of the root, rather than the furthest left child.

    I hope this makes sense. If you need clarification on anything leave a comment or something. Recursion can be quite tricky to get your head around at first.

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

Sidebar

Related Questions

I have the homework question: Find a theta notation for the number of times
I have a homework question that says: Problem 1: Given the array [ 22
I have a homework question please help with a little explanation so that I
I have a question about my C++ homework. I am just confused about *this.
Okay, so I have another question on a prolog homework problem I am struggling
I have a homework question regarding operator precedence in Fortran. In order to understand
I'm stuck with this Java homework question: Write a new method, Refund, that simulates
I have a compiler homework question that wants me to draw a DFA for
I have this homework question: Assume a Database table has 0 records at time
I have the homework question: Explain how a process can refer to objects that

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.