Pergunta de entrevista da empresa Garmin

How would you traverse a binary tree without recursion?

Resposta da entrevista

Sigiloso

20 de dez. de 2012

Just use a stack or a queue, depending on whether you want a depth-first or breadth-first search, respectively. Pseudocode: Enqueue the root node. While the queue is nonempty: Dequeue an element. Skip if visited. Mark it as visited otherwise. Read its value. Do whatever you want with the value (print it, remember it elsewhere, ignore it, etc.). Enqueue all its immediate neighbors. If you were searching for a specific element and you made it this far without finding it, it's not there.

2