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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:49:04+00:00 2026-06-14T08:49:04+00:00

I am trying to recursively display the path taken to a given node in

  • 0

I am trying to recursively display the path taken to a given node in a binary tree where the method will output the path needed in the following way: “left, right, left”.
Here is what I have so far:

public static void pathToNode(BTNode p, char target, String res){
    if(p.data == target){
        res = res + p.data;
        System.out.println(res);
        return;
    }else if(res != null){
        if(res.charAt(0) == 'S'){
        res = res + p.data;
        }
    }else{
        pathToNode(p.leftLink, target, res);
        pathToNode(p.leftLink, target, res);
    }

}

This code is intended to just print out the path like so: “ABCD”.
Having done this I intend on making the method print out either left of right based on the correct option for each node traversal. Any Ideas?

  • 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-14T08:49:05+00:00Added an answer on June 14, 2026 at 8:49 am

    You’re printing the result when you find the target you’re looking for in the first if clause. Alternatively, you should try both the left and the right branches of the tree, what you (almost, you’re doing left twive) do in the last clause.

    I’m not sure what the second clause is supposed to be doing, but removing it and adding p.data to res in the recursive call should make sure the ‘current’ step is saved in the res variable:

    if (p.data == target) {
        res = res + p.data;
        System.out.println(res);
    } else {
        pathToNode(p.leftLink, target, res + p.data);
        pathToNode(p.rightLink, target, res + p.data);
    }
    

    This way, you’re using a traditional base case + recursive case common to recursive algorithms.

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

Sidebar

Related Questions

trying to make a page which will recursively call a function until a limit
I'm trying to list all files and folders recursively under a given folder in
I am trying to generate HTML which will display the data from XML through
I'm trying implement a way to recursively template using jsRender. The issue is, my
I'm trying to write some ruby that would recursively search a given directory for
For a simple project, I'm trying to display log output in a textarea. I
I'm currently trying to recursively loop through a tree structure and serialize it to
How can I recursively scan directories in Android and display file name(s)? I'm trying
I'm trying to write a bash script that will recursively search a directory, find
I'm trying to create aliases for the following commands which recursively convert all file

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.