Google interview question

Print BST by level

Interview Answer

Anonymous

24 Apr 2012

In Java (you call this method with the root): void printBFS(Node s){ Queue q = new Queue(); q.add(s); while (!q.isEmpty()){ Node tmp = (Node)q.remove(); System.out.print(tmp.data + " "); if (tmp.right!=null) q.add(tmp.right); if (tmp.left !=null) q.add(tmp.left ); } }