GeekInterview.com
Series: Subject: Topic:
Question: 13 of 246

Iterative Algorithm

design an iterative algorithm to traverse a binary tree represented in two dimensional matrix
Asked by: Interview Candidate | Asked on: Oct 22nd, 2011
Showing Answers 1 - 1 of 1 Answers
Sandhya.Kishan

Answered On : Jul 16th, 2012

View all answers by Sandhya.Kishan

A binary tree can be traversed using only one dimensional array.

InOrder_TreeTraversal()
{
prev = null;
current = root;
next = null;

while( current != null )
{
if(prev == current.parent)
{
prev = current;
next = current.left;
}

if(next == null || prev == current.left)
{
print current.value;
prev = current;
next = cuurent.right;
}

if(next == null || prev == current.right)
{
prev = current;
next = cuurent.parent;
}

current = next;
}
}

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.