Skip to contentKEEP THE CRAFTTHE SANCTUARY

Binary Tree Level Order

Return the values of a binary tree level by level, from left to right.

The problem

Implement Solution.levelOrder(root). root is the root TreeNode of a binary tree, or None for an empty tree; each node has val, left and right. Return a list with one inner list per depth, starting from the root, where each inner list holds that depth's values from left to right. Return [] for an empty tree.

Starter code

class Solution:
    def levelOrder(self, root):
        pass

Public examples

  1. root = [3, 9, 20, None, None, 15, 7]gives[[3], [9, 20], [15, 7]]
  2. root = [-7]gives[[-7]]
  3. root = [1, 2, 3]gives[[1], [2, 3]]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.