Validate Binary Search Tree (BST)

Algorithms
Medium
Apple
70.7K views

Given the root of a binary tree, determine if it is a valid Binary Search Tree (BST). Check the full range constraints for each node, not just local comparison.

Why Interviewers Ask This

Apple interviewers ask this to evaluate your ability to handle recursive constraints beyond simple local comparisons. They specifically test if you understand that a node's validity depends on the entire history of its ancestors, not just immediate children. This assesses your rigor in edge case handling and your capacity to implement stateful logic within recursion, ensuring robust tree validation under strict engineering standards.

How to Answer This Question

1. Clarify the BST definition: Emphasize that left descendants must be strictly less than the root, and right descendants must be strictly greater, applying globally across the subtree. 2. Define the constraint strategy: Reject the naive approach of comparing only with parents. Instead, propose passing down valid min and max ranges (lower bound and upper bound) as you traverse. 3. Select traversal method: Choose an in-order traversal or a pre-order with range propagation. Explain that pre-order is often clearer for enforcing bounds immediately at each step. 4. Handle edge cases: Explicitly mention how you will handle null nodes, duplicate values (strict inequality), and integer overflow scenarios using long types or specific boundary checks. 5. Analyze complexity: State that the time complexity is O(N) since every node is visited once, and space complexity is O(H) for the recursion stack, where H is tree height. Mention how balanced trees differ from skewed ones.

Key Points to Cover

  • Explicitly rejecting the naive parent-only comparison logic
  • Implementing global range constraints via min/max parameters
  • Demonstrating understanding of strict inequality for duplicates
  • Correctly identifying O(N) time and O(H) space complexity
  • Addressing potential integer overflow edge cases

Sample Answer

To validate a Binary Search Tree, I first need to ensure that the global ordering property holds, not just local parent-child relationships. A common pitfall is checking if a node is greater than its left child and small…

Common Mistakes to Avoid

  • Only comparing the current node with its direct parent instead of global bounds
  • Using non-strict inequalities (>= or <=) allowing duplicate values incorrectly
  • Forgetting to update both min and max bounds when traversing left vs right
  • Ignoring integer overflow risks when dealing with standard integer limits

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 54 Apple questions