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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:29:42+00:00 2026-05-16T04:29:42+00:00

I got this question in an interview with amazon. I was asked to perform

  • 0

I got this question in an interview with amazon.
I was asked to perform a depth first traversal of a tree, without using recursion or stack. I could use a parent pointer for each node, as a part of the structure, but nothing else other than that.(for ex, a “visited” variable” or anything).
Please suggest me an algorithm.

  • 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-16T04:29:42+00:00Added an answer on May 16, 2026 at 4:29 am

    The parent pointer is actually all you need. The trick is to consume the tree as you traverse it.

    Ugly pseudocode:

    cur = treeroot;
    while (1) { // Get to bottom of tree
        if (cur.hasLeft) {
            cur = left;
        } else if (cur.hasRight) {
            cur = right;
        } else {
            break;
    }
    
    // cur is now the bottom node
    
    while (1) {
        doStuff(cur); // output cur, perform op on it, whatever
        if (!cur.hasParent) { // Done with traversal
            break;
        }
        prev = cur; // So we can tell which way we came up to the parent.
        cur = cur.parent;
        if (cur.hasLeft && cur.left == prev) { // Delete left child; it's done
           cur.hasLeft = false;
        } else if (cur.hasRight && cur.right == prev) { // Delete right child; it's done
           // Note: "else" not desirable if a node should not be able to have the same child in two spots
           cur.hasRight = false;
        }
        if (cur.hasLeft) { // Go all the way to the bottom of the left child
            cur = cur.left;
            while (1) {
                if (cur.hasLeft) {
                    cur = cur.left;
                } else if (cur.hasRight) {
                    cur = cur.right;
                } else {
                    break;
                }
            }
        } else if (cur.hasRight) { // Go all the way to the bottom of the right child
            cur = cur.right;
            while (1) {
                if (cur.hasLeft) {
                    cur = cur.left;
                } else if (cur.hasRight) {
                    cur = cur.right;
                } else {
                    break;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got asked this question in a interview. I clearly know what a decorator
Got this as an interview question from Amazon to test basic SQL skills and
Got asked this interesting interview question today. Explain in detail the process by which
This is a question I got asked in an interview. When you create a
I got asked this question on a interview for Google a couple of weeks
Hi I got this asked in an interview question paper. Singleton and Prototype (non-singleton)
I just got this question on an interview and had no idea how to
I got this in an interview question -- the question was more about what
Based on feedback I got from this question , I'm interested in using Lazy
My girlfriend got this question in an interview, and I liked it so much

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.