Motorola Interview Question
Software Engineer / Developers{
private static void alternateLevelOrder(ArrayList<Integer> list, Node root) {
LinkedList<Node> queue = new LinkedList<Node>();
LinkedList<Node> queue2 = new LinkedList<Node>();
boolean reverse = true;
queue2.add(root);
while ((queue.size() + queue2.size()) > 0) {
if (queue.size() > 0) {
Node start = queue.poll();
if (start.left != null) {
// list.add(start.left.data);
queue2.add(start.left);
}
if (start.right != null) {
// list.add(start.right.data);
queue2.add(start.right);
}
} else {
queue.addAll(queue2);
if (reverse) {
Collections.reverse(queue2);
} else {
}
for (Node node : queue2) {
list.add(node.data);
}
queue2.clear();
reverse = !reverse;
}
}
}
}
What si understand is id tree is follow:
then output is: 1 2 3 7 6 5 4.
- SRB April 09, 2011Is this teh expection ?