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

The Archive Base Latest Questions

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

Here is an iterative version of preorder traversal.The idea is to push the root

  • 0

Here is an iterative version of preorder traversal.The idea is to
push the root into a stack.
If left child is not NULL.Push the left child and root=root->left
Else root=Top of stack;Pop;push its right child and root=root->right

Its working on some trees but for some input trees it prints a duplicate value.The behavior of stack is surprising.How does it change from (4 5 6 8) to (4 6 8)?

    void preorder(node* root)
    {stack<node*> st;
     st.push(root);
     cout<<root->val<<" \n ";
     while(!st.empty())
      {if(root->left!=NULL)
         {st.push(root->left);
          cout<<st.top()->val<<" ";
         // printstack(st);
          root=root->left;
          }
       else{root=st.top();
            st.pop();
           if(root->right!=NULL)
            {st.push(root->right);
             cout<<st.top()->val<<" ";   
             //printstack(st);
              root=root->right;
             }
           }
        }
      }

Helper function printstack used to debug

     void printstack(stack<node*> s)
      {stack<node*> t;
      t=s;
      while(!t.empty())
       {cout<<t.top()->val<<" ";
       t.pop();
       }
      } 

For this input tree,the printed stack is below.
The preorder output is
8 3 1 6 5 4 4 7 9


______8_ / \ __3_ 9 / \ 1 ___6 / \ 5 7 / 4

3 8

1 3 8

6 8

5 6 8

4 5 6 8

4 6 8 //How is the Top of stack 4??It should have been 5,Stack being ( 5 6 8)

7 8

  9
  • 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-31T12:57:16+00:00Added an answer on May 31, 2026 at 12:57 pm

    The issue crops up on nodes that have left children but not right. When the node has a right child, that last if block changes the root before the next iteration, but without it, the root doesn’t switch. Then, the next time through, we see a root (which has already been popped of the stack) that has a left child, so it’s left child is pushed on the stack and visited again.

    Hope that helps.

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

Sidebar

Related Questions

Here is my sample code. It is meant to be an iterative procedure for
Here's a problem I ran into recently. I have attributes strings of the form
Here is my code, which takes two version identifiers in the form 1, 5,
I'm not sure whether the use of apply is recommended here. Is there a
Here's to hoping this makes sense. I'm testing a new version of my personal
I've been working on a version of Ian Lunn's interactive map embeded into wordpress.
Here is a simplified version of a stored procedure I am trying to write
I've tried to make an iterative/tail-recursive version of a function to compute the n
I'm trying to implement an iterative version of Tarjan's strongly connected components (SCCs), reproduced
Kind of a dilemma here. I am making a mobile version of a website,

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.