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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:57:27+00:00 2026-05-15T01:57:27+00:00

Here is a java code for breadth-first travel: void breadthFirstNonRecursive(){ Queue<Node> queue = new

  • 0

Here is a java code for breadth-first travel:

void breadthFirstNonRecursive(){
    Queue<Node> queue = new java.util.LinkedList<Node>();
    queue.offer(root);
    while(!queue.isEmpty()){
        Node node = queue.poll();
        visit(node);
        if (node.left != null)
            queue.offer(node.left);
        if (node.right != null)
            queue.offer(node.right);
    }
}

Is it possible to write a recursive function to do the same?

At first, I thought this would be easy, so I came out with this:

void breadthFirstRecursive(){
    Queue<Node> q = new LinkedList<Node>();
    breadthFirst(root, q);
}

void breadthFirst(Node node, Queue<Node> q){
    if (node == null) return;
    q.offer(node);
    Node n = q.poll();
    visit(n);
    if (n.left != null)
        breadthFirst(n.left, q);
    if (n.right != null)
        breadthFirst(n.right, q);   
}

Then I found it doesn’t work. It is actually does the same thing as this:

void preOrder(Node node) {
    if (node == null) return;
    visit(node);
    preOrder(node.left);
    preOrder(node.right);
}

Has any one thought about this before?

  • 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-15T01:57:28+00:00Added an answer on May 15, 2026 at 1:57 am

    I can’t imagine why you’d want to, when you have a perfectly good iterative solution, but here you go 😉

    void breadth_first(Node root):
      Queue q;
      q.push(root);
      breadth_first_recursive(q)
    
    void breadth_first_recursive(Queue q):
      if q.empty() return;
    
      Node n = q.pop()
      print "Node: ", n
      if (n.left) q.push(n.left)
      if (n.right) q.push(n.right)
      breadth_first_recursive(q)
    

    I should add that if you really want to traverse the nodes of the tree recursively, then you could do a DFS with a level parameter, and output nodes only at level, then recurse up. But that’s just crazy talk, because you’d revisit nodes wayyyyy too many times… Just accept that BFS is an iterative algorithm. 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 536k
  • Answers 536k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If sharing the same class in both the projects is… May 17, 2026 at 1:31 am
  • Editorial Team
    Editorial Team added an answer As far as I can see there are two options:… May 17, 2026 at 1:31 am
  • Editorial Team
    Editorial Team added an answer Solution: - put clientaccesspolity.xml and crossdomain.xml under /Web Pages* -… May 17, 2026 at 1:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In this Java code: public class Main { public static void main(String[] args) {
here is working java code class Cup { public String sayColor() { return i
How to inline (here-document) few java code lines into a Ant script ? Please
is it possible in java that code in constructor is called only once even
As a C# developer new to Java, i thought it might be easiest if
i wrote java code in beanshell but it throws java.lang.NoClassDefFoundError by defining DefaultHandler. I
I'm new to Scala, and from what I understand yield in Scala is not
I have been tasked to develop an interactive website using java & mysql: using
i have to use code this.setUp(http://www.remax.com/, *firefox); selenium.open(/404/index.aspx?aspxerrorpath=/public/pages/searchresults.aspx); slenium.click(link=Find a RE/MAX Office); selenium.waitForPageToLoad(30000); selenium.selectFrame(REMAX
I need to communicate between a Java app (desktop) and an iOS app. I've

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.