Grokking 75: Top Coding Interview Questions
Vote

0% completed

Introduction to Tree Breadth First Search Pattern

You are given a binary tree. Return its values grouped by level, so the root is the first group, its children are the second, and so on.

        1
      /   \
     2     3          becomes   [[1], [2, 3], [4, 5, 6]]
    / \     \
   4   5     6

Recursion is the usual way to walk a tree, and here it works against you. A recursive walk goes deep along one branch before coming back, so it meets node 4 before node 3. The values arrive in the wrong order for this question.

What you need is the opposite. Visit everything at depth 1, then everything at depth 2, and never skip ahead.

.....

.....

.....

Like the course? Get enrolled and start learning!
Ahmed k

Ahmed k

· 4 years ago

Nothing about construction and conversion?