Amazon Interview Question
InternsCountry: India
Interview Type: In-Person
public void breadthFirstTraversal (TreeNode localRoot)
{
inbulitQueue qu = new inbulitQueue(10);
qu.add(localRoot);
while (!qu.isEmpty())
{
TreeNode temp = qu.remove();
if(temp==null)
break;
System.out.println("BFS traversal is: "+temp.data);
if (temp.leftchild != null)
qu.add(temp.leftchild);
if (temp.rightchild!=null)
qu.add(temp.rightchild);
}
}
Same as BFS on a graph (without the need for "visited" flags):
Let there be a Queue Q ....
- Sri September 30, 2013