You only have free questions left (including this one).

But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.

You're building a tool that draws binary trees on the screen, one row at a time. To draw a row, it needs the values of all the nodes at that depth, in order from left to right.

Write a function get_levels that takes the root of a binary tree and returns its values level by level, as a list of lists. The first level holds just the root's value, the next level holds the values of the root's children, then their children's values, and so on. Within each level, the values go from left to right.

This is called a level order traversal of the tree. It's also called a breadth-first traversal.

If the tree is empty (the root is None), return an empty list.

Here's a sample binary tree node class:

class BinaryTreeNode(object): def __init__(self, value): self.value = value self.left = None self.right = None def insert_left(self, value): self.left = BinaryTreeNode(value) return self.left def insert_right(self, value): self.right = BinaryTreeNode(value) return self.right

For example, if root is the root of this tree:

depth 0: 8 / \ depth 1: 3 10 / \ \ depth 2: 1 6 14 / \ / depth 3: 4 7 13

then:

get_levels(root) # [[8], [3, 10], [1, 6, 14], [4, 7, 13]] get_levels(None) # []

Nodes on the same level don't always have the same parent. In the example, 4 and 7 are children of 6, but 13 is a child of 14, way over on the other side of the tree. They're all at depth 3, so they all go in the same list.

Not every node has two children. Make sure missing children don't show up in your result as placeholder values or empty levels.

Careful with an empty tree. Your function should return an empty list, not [[]] (a list holding one empty level).

We can do this in O(n) time and O(n) space, where n is the number of nodes in the tree.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

O(n) time and O(n) space, where n is the number of nodes in the tree.

Each node goes into the queue once and comes out once. While it's out, we do a constant amount of work: we record its value and check its two children. So the whole traversal takes O(n) time.

For space, our result holds every value in the tree, so it's O(n).

What about the queue? At the start of each level, it holds that whole level, so its size depends on how wide the tree is. The worst case is a complete binary tree where every level is full. About half of its nodes are on the bottom level (for example, 8 of the 15 nodes in a full tree with 4 levels), and when we get to that level, all of them are in the queue at once. So even if we didn't count our result, the queue alone could take O(n) space.

Our depth-first version used extra space proportional to the tree's height instead: O(log n) for a balanced tree, but O(n) for a tree that's one long line. A breadth-first traversal is the opposite: a long line only ever has one node in the queue, but a full tree has about n/2. Either way, the result itself takes O(n) space.

We can't do better than O(n) time. We have to return every value in the tree, so we have to visit every node.

  1. Zigzag order. Return the levels in zigzag order: left to right for the first level, right to left for the next one, then left to right again, and so on. Do you need to change how you traverse the tree, or just how you build each level?
  2. Right side view. If you stood to the right of the tree, which nodes could you see? Return the value of the rightmost node on each level. (For the example tree, that's 8, 10, 14, and 13.) Can you do it without building every level?
  3. Average of each level. Return the average of the values on each level. (For the example tree, that's 8, 6.5, 7, and 8.)
  4. Minimum depth. Find the depth of the shallowest leaf (a node with no children). In the example tree, that's 1, at depth 2. Why can a breadth-first traversal often stop much sooner than a depth-first one here?

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

. . .