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
- 244. Given a 4x4 board with pieces in it ....
- Given number 1,2,3,4,5,6,7,8,9 find all sets that sums up to 10.....
- How could you determine if a linked list contains a cycle in it, and, at what node the cycle starts?
- 169. design a data structure for a DNS server
- Given a singly linked list with a loop ......
- Whats a Linked List ? Given Two linked list forming a Y shaped linked list ...
- Given a matrix of integers sorted along each row and each column, implement a rapid search algorithm
- 178. Given a very large array of n integers.......
- Preparing sets from a given array with min complexity.........
- 199. Given 5/2=3, what will be the output of 10/3?
- More...
|