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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:56:46+00:00 2026-06-09T23:56:46+00:00

I have two algorithms here that mirror a BST. Both of them work fine,

  • 0

I have two algorithms here that mirror a BST. Both of them work fine, however as far as I understand the second one is recursive and the first one is tail recursive. I know in Java, the compiler doesn’t optimize for tail recursion, but my question is — in any other language, what would be the better algorithm out of these two? Is this too subjective of a question?

public Node mirror(Node root, Node newRoot) {
    newRoot.item = root.item;
    if (root.right != null) {
        newRoot.left = new Node();
        newRoot.left.item = root.right.item;
        mirror(root.right, newRoot.left);
    }
    if (root.left != null) {
        newRoot.right = new Node();
        newRoot.right.item = root.left.item;
        mirror(root.left, newRoot.right);
    }
    return newRoot;
}

///VERSION 2////

public Node mirror(Node currentNode) {
    if (currentNode == null) {
        return null;
    } else {
        Node newnode = new Node();
        newnode.item = currentNode.item;
        newnode.left = mirror(currentNode.right);
        newnode.right = mirror(currentNode.left);
        return newnode;
    }
}
  • 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-09T23:56:47+00:00Added an answer on June 9, 2026 at 11:56 pm

    A few things:

    First, many imperative languages don’t optimize for tail recursion. That’s something very common for functional languages, and you should use one of those if you want this feature. Scala might be a good move, since it uses the JVM. Also, with Scala you need to specifically annotate what you want to make tail recursive

    Second, the second piece of code isn’t tail recursive. Tail recursion is a fairly strong requirement: the recursive call must be the very last thing executed if the code were interpreted in continuation passing style. In practice, what that means is that your return statement must be the only thing that does recursion. For example, in C-like pseudocode:

    int factorial(int x) {
      if (x == 0) return 1;
      else return x*factorial(x-1);
    }
    

    This is not tail recursive because the multiplication needs to be done after the recursive call but before the return statement. Here is a tail recursive version:

    int factorial_helper(int acc, int x) {
       if (x == 0) return acc;
       else return factorial_helper(acc*x, x-1);
    }
    
    int factorial(int x) { return factorial_helper(1, x); }
    

    You can see in factorial_helper that the value returned by the recursive call is exactly the value returned by the function — and that’s what make this tail recursive.

    In your second algorithm, there are two recursive calls. In each recursive call the address of the mirrored tree is stored in the newnode. Then the address of the newnode is returned (Java secretly has pointers). So, this is not written tail-recursively because you’re not returning the exact result of your recursive call.

    Third: for something like this, the best way to see which performs better is to run both and benchmark :). I don’t see any clear winner from the algorithms alone (maybe someone else does?). It doesn’t look like there’s going to be any asymptotic difference, although rewriting this to be actually tail recursive may speed things up.

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

Sidebar

Related Questions

I only have two variables to work with here, age (unix timestamp) and hits.
I have this code here that has two arrays. It sorts arr[] , so
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
suppose you are given the following problem. You have two index sets that have
I have a number of algorithms for community detection on graphs that I want
I have 3 main questions about the algorithms in intelligent web (web 2.0) Here
Here is the problem. I have rectangular canvas that have size of 1. So
Dictionary cannot have two values with same key. please tell which logic/algorithm being used
I have applied Two different Image Enhancement Algorithm on a particular Image and got
Have two folders with approx. 150 java property files. In a shell script, how

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.