Interview Questions

155. Print the nodes of a binary tree by level (breadth first)

Microsoft Interview Questions and Answers


(Continued from previous question...)

155. Print the nodes of a binary tree by level (breadth first)

Question:
Print the nodes of a binary tree by level (breadth first).


maybe an answer:


//Breadth First Search

public void bfs()
{
//BFS uses Queue data structure
Queue q=new LinkedList();
q.add(this.rootNode);
printNode(this.rootNode);
rootNode.visited=true;
while(!q.isEmpty())
{
Node n=(Node)q.remove();
Node child=null;
while((child=getUnvisitedChildNode(n))!=null)
{
child.visited=true;
printNode(child);
q.add(child);
}
}
//Clear visited property of nodes
clearNodes();
}

(Continued on next question...)

Other Interview Questions