InferenceAI

Enter at least two characters to search.

Start free

Tree Height

easyTrees15 min10 XP

Return the maximum depth of a binary tree: the number of nodes on the longest path from the root to a leaf. An empty tree has depth 0.

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]]
TASKtree_height()
EXPECTED OUTPUT3

Start with the input values shown in this example.

Example 1 of 3

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

Output: 3

Explanation: balanced

Input: values = [1,null,2]

Output: 2

Explanation: right chain

Input: values = []

Output: 0

Explanation: empty

Hint 1

Depth = 1 + max(depth(left), depth(right)).

Hint 2

Breadth-first search also works: count the levels.

Topics

DSATreesPython