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 in!
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:
For example, if root is the root of this tree:
then:
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.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
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.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Reset editor