InferenceAI

Enter at least two characters to search.

Start free

Tree Levels

mediumTrees25 min25 XP

Return the values of a binary tree level by level, left to right, as a list of lists.

Trees are given in level order as a list, with None for a missing child (for example [3, 9, 20, None, None, 15, 7]).

Examples

EXAMPLE WALKTHROUGHthree levels

A guided view of the published example, not a trace of your code.

INPUT[[3,9,20,null,null,15,7]]
TASKtree_levels()
EXPECTED OUTPUT[[3],[9,20],[15,7]]

Start with the input values shown in this example.

Example 1 of 3

Input: values = [3,9,20,null,null,15,7]

Output: [[3],[9,20],[15,7]]

Explanation: three levels

Input: values = [1]

Output: [[1]]

Explanation: single node

Input: values = []

Output: []

Explanation: empty

Hint 1

Process the tree one level at a time with a queue.

Topics

DSATreesPython