validate-binary-search-tree.sh — zsh

Validate Binary Search Tree

medium
binary-treebinary-search-treedepth-first-search

Return true if every node in root satisfies the strict binary-search-tree ordering rule.

Constraints

  • 1 <= number of nodes <= 10000
  • -2^31 <= node value <= 2^31 - 1

Follow-up

Why is checking only each node's immediate children insufficient?

Examples
Example 1
Input: root = [2,1,3]
Output: true
Example 2
Input: root = [5,1,4,null,null,3,6]
Output: false
Example 3
Input: root = [1]
Output: true
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.