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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:58:11+00:00 2026-05-26T17:58:11+00:00

I was wondering if it’s possible to print a binary tree in breadth first

  • 0

I was wondering if it’s possible to print a binary tree in breadth first order while using only O(1) space?

The difficult part is that one have to use additional space to memorize the next level to traverse, and that grows with n.

Since we haven’t place any limitation on the time part, maybe there are some inefficient (in terms of time) ways that can achieve this?

Any idea?

  • 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-26T17:58:12+00:00Added an answer on May 26, 2026 at 5:58 pm

    This is going to depend on some finer-grained definitions, for example if the edges have back-links. Then it’s easy, because you can just follow a back link up the tree. Otherwise I can’t think off hand of a way to do it without O(lg number of nodes) space, because you need to remember at least the nodes “above”.

    Update

    Oh wait, of course it can be done in O(1) space with a space time trade. Everywhere you would want to do a back link, you save your place and do BFS, tracking the most recent node, until you find yours. Then back up to the most recently visited node and proceed.

    Problem is, that’s O(1) space but O(n^2) time.

    Another update

    Let’s assume that we’ve reached node n_i, and we want to reach the parent of that node, which we’ll call wlg n_j. We have identified the distinguished root node n_0.

    Modify the breath-first search algorithm so that when it follows a directed edge (n_x,n_y), the efferent or “incoming” node is stored. Thus when you follow (n_x,n_y), you save n_x.

    When you start the BFS again from n_0, you are guaranteed (assuming it really is a tree) that at SOME point, you will transition the edge (n_j,n_i). At that point you observe you’re back at n_i. You’ve stored n_j and so you know the reverse edge is (n_i,n_j).

    Thus, you get that single backtrack with only two extra cells, one for n_0 and one for the “saved” node. This is O(1)

    I’m not so sure of O(n^2) — it’s late and it’s been a hard day so I don’t want to compose a proof. I’m sure it’s O((|N|+|E|)^2) where |N| and |E| are the size of the sets of vertices and edges respectively.

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

Sidebar

Related Questions

Wondering if it's possible to print a carriage return without a line feed in
Wondering if it's possible to create a dynamic linq query using linq to objects.
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
Wondering if this is possible. We have an 3rd Party library that contains identification
Wondering why a particular binary write operation in VB is so slow. The function
Wondering if it's possible to change a value returned from a queryset before sending
Wondering if anyone has set up GZIP compression for Spring HttpInvoker requests using Tomcat,
Wondering if anyone has had any experience using retina assets with the NativeControls iPhone
Wondering if it is possible for my claims aware application (ASP.NET) to save a
Wondering what a continue statement does in a do...while(false) loop, I mocked up a

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.