LeetCode Problem

How to Solve Validate Binary Search Tree

To validate if a binary tree is a valid Binary Search Tree (BST), we traverse it using Depth-First Search (DFS). During traversal, track node values to check if each node falls within the valid range. This approach ensures that we meet the BST properties and maintain valid ranges for each node.

GhostInterview Help

Need help with Validate Binary Search Tree without spending extra time grinding it?

GhostInterview can read Validate Binary Search Tree from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #98Binary-tree traversal and state trackingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Binary-tree traversal and state tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To validate if a binary tree is a valid Binary Search Tree (BST), we traverse it using Depth-First Search (DFS). During traversal, track node values to check if each node falls within the valid range. This approach ensures that we meet the BST properties and maintain valid ranges for each node.

Problem Statement

Given the root of a binary tree, you need to determine whether the tree is a valid Binary Search Tree (BST). A valid BST is one where for each node, the left subtree contains only nodes with values less than the node’s value, and the right subtree contains only nodes with values greater than the node’s value. This must hold true for every node in the tree.

A Binary Search Tree (BST) must meet these conditions recursively for every node in the tree. You are tasked with traversing the tree while maintaining boundaries that ensure the values of the nodes follow the BST properties. The tree is valid if and only if all nodes satisfy these conditions.

Examples

Example 1

Input: root = [2,1,3]

Output: true

Example details omitted.

Example 2

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

Output: false

The root node's value is 5 but its right child's value is 4.

Constraints

  • The number of nodes in the tree is in the range [1, 104].
  • -231 <= Node.val <= 231 - 1

Solution Approach

Tree Traversal with State Tracking

Start by performing a Depth-First Search (DFS) traversal of the binary tree. While traversing, maintain the valid range for each node. The left child must have values smaller than the current node’s value, and the right child must have values larger. Update these ranges recursively as you explore each node. If any node fails this condition, the tree is invalid.

Recursive Approach

Using recursion, for each node, check if it falls within the range set by its ancestors. The range for a left child will be bounded by the node's value from the parent, and for the right child, the upper bound will be the node's value. If the node violates its bounds, the tree is invalid.

Efficient Time and Space Complexity

The time complexity for this approach is O(N), where N is the number of nodes in the tree, as each node is visited exactly once. The space complexity is O(H), where H is the height of the tree, due to the recursion stack. In the worst case, if the tree is unbalanced, H could be O(N), but in the best case, it’s O(log N).

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is O(N), where N is the number of nodes, because we visit each node once. The space complexity depends on the height of the tree, O(H), due to the recursion stack. In the worst case (unbalanced tree), this could be O(N), but in a balanced tree, it’s O(log N).

What Interviewers Usually Probe

  • Do you understand the properties of a valid Binary Search Tree?
  • Can you explain how recursion helps track valid ranges for nodes?
  • Will you describe the time and space complexity of this solution?

Common Pitfalls or Variants

Common pitfalls

  • Not correctly maintaining the range for each node during recursion.
  • Misunderstanding how the left and right subtrees' valid ranges differ.
  • Failing to account for edge cases like a tree with only one node.

Follow-up variants

  • Check if a binary tree is a valid AVL tree (self-balancing binary search tree).
  • Determine if a binary tree is a valid Binary Search Tree with duplicate values allowed.
  • Validate a Binary Search Tree using iterative traversal techniques.

How GhostInterview Helps

  • Capture the initial input of a binary tree and instantly evaluate its validity.
  • Provide an answer path that clearly explains traversal logic and the validation process, including complexity analysis.
  • Support live screen-sharing workflows to walk through the code, visually tracking recursion and ranges.

Topic Pages

FAQ

What makes a binary tree a valid Binary Search Tree?

A Binary Search Tree is valid if, for every node, its left child contains values smaller than the node and the right child contains values greater, with this property recursively applying to every node in the tree.

How does the recursive approach work to validate a BST?

In the recursive approach, each node’s value is validated by ensuring it falls within a valid range that is updated as we traverse down the tree. The left child should be smaller, and the right child should be larger.

What is the time complexity of this problem?

The time complexity is O(N), where N is the number of nodes in the tree, since each node is visited once during the DFS traversal.

Can this solution handle edge cases like empty or single-node trees?

Yes, the solution works for edge cases like an empty tree or a single node. In these cases, the tree is automatically considered valid.

How do you handle unbalanced trees in this problem?

The solution handles unbalanced trees effectively by ensuring that each node’s left and right children respect the required bounds, regardless of the tree's structure.

GhostInterview Solver

Need direct help with Validate Binary Search Tree instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Validate Binary Search Tree from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.