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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:20:17+00:00 2026-05-28T19:20:17+00:00

I have this function to find all the paths from root to leaf in

  • 0

I have this function to find all the paths from root to leaf in a BST.

public static void paths(Node node, LinkedList<Integer> list) {

    if (node == null) {
        return;
    }
    list.add(node.data);

    if (node.left == null && node.right == null) {
        print(list);
        return;
    } else {
        paths(node.left, list);
        paths(node.right, list);
    }
}

public static void print(LinkedList<Integer> list1) {
    System.out.println("Contents of list: " + list1);
}

I call it using:

LinkedList list = new LinkedList();
paths(bt.root, list);

e.g:

07
02
01 05

It prints:

7 2 1
7 2 1 5 [instead of 7 2 5]

Somehow the value 1 is retained in the “list” even after it returns from the recursion.

  • 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-28T19:20:18+00:00Added an answer on May 28, 2026 at 7:20 pm

    As others have said, there’s only a single LinkedList object in use here. xyz incorrectly described this as using pass by reference, but it’s actually passing the value of list which is a reference, by value. (Changes to the parameter itself, such as assigning it a different value, aren’t visible to the caller; changes within the object that the parameter refers to are visible.)

    It’s really important that you understand how variables, objects and references work in Java. When you write (say):

    Node node = new Node();
    

    … then the value of node is not a Node object. It’s a reference to a Node object. Assignment and parameter passing deal with that value (the reference) not the object. So for example:

    Node node1 = new Node();
    Node node2 = node1;
    

    node1 and node2 now refer to the same object – there’s no object copying involved.

    As for how you can fix this, there are two options that spring to mind:

    • Create a copy of the linked list on each recursive step, so that changes are independent
    • “Pop” the newly added value off the end of the linked list at the end of the method, so that when a method of recursion depth n completes, the linked list is back to having n-1 elements.

    You can do this by simply adding a call to:

    list.removeLast();
    

    either in both branches, or in a finally block, or removing the explicit return:

    if (node.left == null && node.right == null) {
        print(list);
        // Note no return statement
    } else {
        paths(node.left, list);
        paths(node.right, list);
    }
    list.removeLast();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out where this function comes from. Any one have any
I have this function to read in all ints from the file. The problem
I have been unable to find any decent documentation on this function. The code
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
I have this function from a plugin (from a previous post) // This method
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
I have this function in my Javascript Code that updates html fields with their
I have this function in my head: <head> window.onload = function(){ var x =
I have this function... private string dateConvert(string datDate) { System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo(en-GB);
I have this function: RegisterGlobalHotKey(Keys.F6, MOD_SHIFT | MOD_CONTROL); which call an API to register

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.