Google Interview Question

Given a binary tree, print out the whole tree by levels

Interview Answers

Anonymous

Dec 18, 2013

BFS

4

Anonymous

May 10, 2014

Java: public static void printTreeByLevels(Tree t) { if (t == null) return; Queue<div>q = new LinkedList(); q.add(t); while (!q.isEmpty()) { Tree node = q.poll(); if (node == null) continue; System.out.println(node.value); q.add(node.left); q.add(node.right); } }</div>

3

Anonymous

Jan 21, 2013

Good answer here http://leetcode.com/2010/09/printing-binary-tree-in-level-order.html

3

Anonymous

Jan 15, 2013

you may need to divide the problem in 2: 1) prepare the data (recursive) and save all the binary tree (in a Map) by level 2) go through the Map and print it out

3