Amazon Interview Question
Software Engineer / Developersnothing so special about the solution!!!!go through the recursive versions of each traversal and simply merge all the solutions
private void traverse(Node h)
{
if (h == null) return;
h.item.visit();
traverse(h.l);
traverse(h.r);
}
void trav()
{ traverse(root); }
By default the code implements a preorder traversal; if we put the method h.item.visit() between the recursive calls, we have an inorder traversal; and if we put it after the recursive calls, we have a postorder traversal.
traverse(s)
- Cougar_CS October 14, 2008{
if(s) {
preorder_queue.add(s);
traverse(s->left);
inorder_queue.add(s);
traverse(s->right);
postorder_queue(s);
}
}