Level Order Tree Traversal in F#

Anoth­er com­mon prob­lem I see dur­ing my prepa­ra­tion for tech­ni­cal inter­views is the Lev­el Order Tree Tra­ver­sal (or Breadth-First) prob­lem.

I always like to think with my F# hat on when I solve algo­rithm prob­lem, so here’s an imple­men­ta­tion in F# .

Here we have a nest­ed recur­sive loop func­tion that:

  • yields the val­ue from the nodes on the cur­rent lev­el
  • then find all the chil­dren of the nodes on the cur­rent lev­el and recurse with them

 

Try it Yourself

 

Links