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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:01:00+00:00 2026-05-25T03:01:00+00:00

This question relates to recursion. Consider the program shown below (not my real code,

  • 0

This question relates to recursion. Consider the program shown below (not my real code, but this explains the problem I have).

The function must use recursion as shown, and what I want to do is have a way that each leaf value instead of getting printed out, is saved to a list. So finally I get a List<String> that when I print out gives me the contents of each leaf node.

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<title text=\"title1\">\n" +
"    <comment id=\"comment1\">\n" +
"        <data> abcd </data>\n" +
"        <data> efgh </data>\n" +
"    </comment>\n" +
"    <comment id=\"comment2\">\n" +
"        <data> ijkl </data>\n" +
"        <data> mnop </data>\n" +
"        <data> qrst </data>\n" +
"    </comment>\n" +
"</title>\n";

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));

List<String> results = traverse(doc.getFirstChild());

//Want to print out results list here...

public static List<String> traverse(Node node){
    System.out.println(node.getNodeName());
    for(int i = 0; i < node.getChildNodes().getLength(); i++){
        traverse(node.getChildNodes().item(i));         
    }
    return null;
}

So my question is, how can I re-write the traverse function in a way that, it still uses recursion but saves all leaf nodes to the list. And then it returns the list of all values.

  • 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-25T03:01:01+00:00Added an answer on May 25, 2026 at 3:01 am

    This one stores in a List the same Strings you print with your traverse function and you don’t need to pass the List as an argument:

    public static List<String> traverse( Node node ) {
            List<String> results = new LinkedList();
            results.add(node.getNodeName());
            if ( node.getChildNodes().getLength() > 0 ) {
                for ( int i = 0; i < node.getChildNodes().getLength(); i++ )
                    results.addAll(traverse(node.getChildNodes().item(i)));
            }
            return results;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is not a real programming question. But, it relates to programming
Note: this question relates to stack overflows (think infinite recursion), NOT buffer overflows. If
Background: This question relates to versions of Delphi below 2009 (ie without Unicode support
This is kind of a two part question. But it both relates to the
This question is related to (but perhaps not quite the same as): Does Django
This question relates to Django Aggregation/Annotation in 1.1. Suppose I have a simple model
This question relates to ASP.NET MVC 2 RC (December drop). The basic problem is
This question relates to Java collections - specifically Hashtable and Vector - but may
Please note: This question relates to the Appcelerator Titanium platform, not the stock iOS
This question relates to this question . I have a EF model like this

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.