InferenceAI

Enter at least two characters to search.

Start free

Balanced Tree Check

easyTrees15 min10 XP

Return True if the binary tree is height-balanced: for every node, the heights of its left and right subtrees differ by at most 1.

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 WALKTHROUGHbalanced

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

INPUT[[3,9,20,null,null,15,7]]
TASKbalanced_tree_check()
EXPECTED OUTPUTtrue

Start with the input values shown in this example.

Example 1 of 3

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

Output: true

Explanation: balanced

Input: values = [1,2,2,3,3,null,null,4,4]

Output: false

Explanation: unbalanced

Input: values = []

Output: true

Explanation: empty

Hint 1

Return the height bottom-up, using -1 as a signal that a subtree is already unbalanced.

Topics

DSATreesPython