InferenceAI

Enter at least two characters to search.

Start free

Valid Search Tree

mediumTrees25 min25 XP

Return True if the tree is a valid binary search tree: every value in a node's left subtree is strictly smaller, and every value in its right subtree is strictly larger, at every 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 WALKTHROUGHvalid

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

INPUT[[2,1,3]]
TASKvalid_search_tree()
EXPECTED OUTPUTtrue

Start with the input values shown in this example.

Example 1 of 3

Input: values = [2,1,3]

Output: true

Explanation: valid

Input: values = [5,1,4,null,null,3,6]

Output: false

Explanation: right subtree too small

Input: values = [5,4,6,null,null,3,7]

Output: false

Explanation: deep violation

Hint 1

Checking only direct children is not enough.

Hint 2

Pass down the allowed (low, high) range for each subtree.

Topics

DSATreesPython