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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:19:20+00:00 2026-06-12T01:19:20+00:00

i am trying to learn abit about recursive methods and is writing a method

  • 0

i am trying to learn abit about recursive methods and is writing a method for my binary tree that counts the sum of all the integers in the tree my code works fine and all but i am still abit confused about how the application knows when to stop. my code looks like the this:

    public int sum(){

    return sum(overallRoot);
}

private int sum(IntTreeNode root) {
    if (root == null) {
        return 0;
    }else {
        return root.data + sum(root.left) + sum(root.right);
    }

}

(the above code is from my nodeTree class)

The next code is from my main class:

public class TreeClient {

/**
 * @param args
 */
public static void main(String[] args) {
    IntTree tree = new IntTree(12);
    System.out.println(tree.sum());
}

}

So the question is (maybe for many quite simple) but how does my application know when to stop? ive tried with simple system out prints to figur out but as far as my understanding is right now the method would call it self in an endless loop?

hope someone has time to respond!

  • 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-12T01:19:22+00:00Added an answer on June 12, 2026 at 1:19 am

    In any recursive program, your iteration stops when a base condition is reached.. Here your base condition is : –

    if (root == null) {
        return 0;
    }
    

    So, when your root.left and root.right, in the following return statement in else block both becomes null, you have reached your base condition, and hence your loop stops..

    return root.data + sum(root.left) + sum(root.right);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to learn a bit about PDO and is going through this tutorial .
Im trying to learn a bit about c++ and have run in to some
I'm trying to learn a bit about html5 animation, and have come up with
I'm trying to learn more about WPF and I've read a bit about Model-View-ViewModel
Alright, I'm just trying to learn about using Contact information, but I'm a bit
I'm trying learn more about RavenDB and at the moment to focus on how
I was trying to learn a bit about h264 by looking at the bitstream
Right now i am trying to learn more about java threading, and i have
I'm trying to learn a bit about dynamic programming and had a peice of
I am trying to learn a bit about optimizing my jQuery code... Is there

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.