Overview
Read a binary tree row by row, top to bottom, with a queue and one count per round that keeps the rows apart.
- a node with left and right children
- a queue visits a tree row by row
- take len(queue) nodes per row
Short Course
A binary tree node has a value and up to two children. To read it row by
row, use a queue and handle exactly len(queue) nodes per round: those are
the current row. Trap: without that count, the rows run together.
# Tree(value, left, right) is in the starter
root = Tree(1, Tree(2), Tree(3, Tree(4)))
root.left.value # 2
root.right.left.value # 4
Task
Write levels(root): one list of values per row, top to bottom, left to
right. Example: for the tree above, [[1], [2, 3], [4]]; for None, [].
Test Suites
Pending
Next
Output