Question d’entretien chez Garmin

How would you traverse a binary tree without recursion?

Réponse à la question d'entretien

Utilisateur anonyme

20 déc. 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