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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:59:22+00:00 2026-06-06T15:59:22+00:00

What does this function do? And how do you evaluate it? How to trace

  • 0

What does this function do? And how do you evaluate it?

How to trace it? How to understand recursive method?

I just can’t understand the recursion, and the exam date is coming soon, and I know in order to understand recursion, I must first understand recursion.

But I just can’t write a slightly complex recursive method, can anyone help me out using the simplest English words.

public class BalancedStrings {

    public static void printBalanced(String prefix, int a, int b) {
        if (a > 0)
            printBalanced(prefix + "a", a - 1, b);
        if (b > 0)
            printBalanced(prefix + "b", a, b - 1);
        if (a == 0 && b == 0)
            System.out.println(prefix);
    }

    public static void printBalanced(int n) {
        if (n % 2 == 0) {
            printBalanced("", n / 2, n / 2);
        }
    }

    public static void main(String[] args) {
        printBalanced(4);
    }
}
  • 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-06T15:59:24+00:00Added an answer on June 6, 2026 at 3:59 pm

    The calls to printBalanced() are the recursive calls. The way to recognize recursion is when a function is calling itself. Recursion is best understood when drawn using a tree:

    enter image description here

    The branches of the tree will continue to create more functions until an end condition is met, which in this case is a == 0 && b == 0. The function you provided looks like it is printing the string ‘prefix’ concatenated with a specified number of ‘a’ and ‘b’ characters, recursively. When the variables a and b reach 0, the recursion stops and the result is printed using System.out.println(prefix);

    In the main function you are passing the integer 4 to printBalanced(int n) which the calls printBalanced(String prefix, int a, int b) with parameters like this: printBalanced("",2,2);

    The overall result is prefix concatenated with a balanced number of a’s and b’s

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

Sidebar

Related Questions

Does anybody know why this function, when passed an invalid date (e.g. timestamp) to
Does anyone know how I can reverse this function to produce the original input
Can someone elaborate how this function works? Does it load every time from disk
Does VS 2008 provides a function that allows us to evaluate a method? I
* Simple question : Why does this function throw an exception when evaluated ?
Does this function has the same behavior that memset ? inline void SetZeroArray( void
Why does this work.... function Person(name) { this.name = name; } Person.prototype.speak = function()
why does this only alert 1 ? function test() { var myobj = {
I only want this function to run if .toolbar li does not have the
What does this pattern '/\\\(?!&#|\?#)/' match in PHP preg_replace function? Is this pattern valid?

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.