InferenceAI

Enter at least two characters to search.

Start free

Sorted List to Balanced BST

easyTrees15 min10 XP

Build a height-balanced binary search tree from the sorted list nums, always choosing the left middle element nums[(lo + hi) // 2] as the root of each range. Return the tree's values level by level.

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 WALKTHROUGHfive values

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

INPUT[[-10,-3,0,5,9]]
TASKsorted_list_to_balanced_bst()
EXPECTED OUTPUT[[0],[-10,5],[-3,9]]

Start with the input values shown in this example.

Example 1 of 3

Input: values = [-10,-3,0,5,9]

Output: [[0],[-10,5],[-3,9]]

Explanation: five values

Input: values = [1,3]

Output: [[1],[3]]

Explanation: two values

Input: values = []

Output: []

Explanation: empty

Hint 1

The middle element is the root; recurse on each half.

Topics

DSATreesPython