TIC
The Interns Company

Validate Binary Search Tree

MediumAcceptance: 30.2%

Problem Statement

Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.

Constraints:

  • The number of nodes in the tree is in the range [1, 10^4]
  • -2^31 <= Node.val <= 2^31 - 1

Input Format:

  • The root of a binary tree

Output Format:

  • Return true if the given tree is a valid BST, false otherwise

Examples:

Example 1:

Input:

root = [2,1,3]

Output:

true

Explanation:

The tree looks like: 2 / \ 1 3 The left subtree value 1 is less than the root value 2, and the right subtree value 3 is greater than the root value 2.

Example 2:

Input:

root = [5,1,4,null,null,3,6]

Output:

false

Explanation:

The tree looks like: 5 / \ 1 4 / \ 3 6 The value 3 is less than the root value 5, which violates the rule that all nodes in the right subtree must have values greater than the root value.

Example 3:

Input:

root = [5,4,6,null,null,3,7]

Output:

false

Explanation:

The tree looks like: 5 / \ 4 6 / \ 3 7 The value 3 is less than the root value 5, which violates the rule that all nodes in the right subtree must have values greater than the root value.

Solutions

Recursive Approach with Range Checking

Time: O(n)
Space: O(h)

Use recursion to traverse the tree, maintaining a valid range for each node. Each node's value must be within the allowable range for the tree to be a valid BST.

In-order Traversal

Time: O(n)
Space: O(h)

Perform an in-order traversal of the tree, which should yield values in ascending order for a valid BST. If at any point the current value is less than or equal to the previous value, the tree is not a valid BST.

Algorithm Walkthrough

Example input: nums = []

Step-by-step execution:

  1. Start validation of root node (5) with range (-Infinity, Infinity)
  2. Validate left subtree (1) with range (-Infinity, 5) - Valid
  3. Validate right subtree (4) with range (5, Infinity)
  4. Node value 4 is less than 5, so it's in the correct range
  5. Validate right subtree's left child (3) with range (5, 4)
  6. Node value 3 is not within range (5, 4) as 3 < 5 - Invalid
  7. Return false as the tree violates BST property

Hints

Hint 1
Think about using recursion to traverse the tree and validate each subtree.
Hint 2
Remember that it's not sufficient to just check if left < node < right for each node; you need to ensure all nodes in the left subtree are less than the current node, and all nodes in the right subtree are greater.
Hint 3
Consider using a range for each node (min and max values) to validate if it meets the BST requirements.

Video Tutorial

Video tutorials can be a great way to understand algorithms visually

Visualization

Visualize the recursive validation process, showing the valid range for each node and highlighting when a node violates the BST property.

Key visualization elements:

  • current node
  • valid range
  • violation

Implementation Notes

This problem tests understanding of Binary Search Tree properties and tree traversal techniques. The key insight is recognizing that each node must be within a valid range, not just compared to its immediate children.