Microsoft Interview Question






Comment hidden because of low score. Click to expand.
1
of 1 vote

Absolutely..using a queue plus a stack is the way to do this.
You can do it like this..
Read the root value. Add it to the queue. Then delete the element from the queue, add it to the STACk and add the Right child and Left child to the queue. Keep doing this until the queue is empty. POP the STACk and you have your answer.

- Anonymous August 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

won't work... check for yourself...

- Adi October 16, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u give a test case

- game August 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It works.

- russoue February 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think their is a small mistake in the algo as per this algo it will print 7654321 but we need 4567231. for this algo first add right child and then add left child in the queue..

- thinker June 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Only one data structure <deque> is needed.
Test passed.

void print(Node* node){
deque<Node*> deq;
if (!node)
return;
deq.push_back(node);
for (deque<Node*>::const_iterator it=deq.begin();it!=deq.end();++it){
if ((*it)->left!=NULL)
deq.push_back((*it)->left);
if ((*it)->right!=NULL)
deq.push_back((*it)->right);
}

for (deque<Node*>::reverse_iterator it=deq.rbegin();it!=deq.rend();++it)
cout<<(*it)->value;

}

- XYZ September 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 0 vote

void Tree::PrintLevels(TNode* root)
{
if( root == NULL)
return;

queue<TNode*> q;
stack<int> st;
q.push(root);

while(!q.empty())
{
TNode* node = (TNode*)q.front();
q.pop();
st.push(node->data);
if(node->right)
q.push(node->right);
if(node->left)
q.push(node->left);
}

while(!st.empty())
{
printf("%d",st.top());
st.pop();
}
}

- Ramu October 05, 2007 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More