Validate Binary Search Tree (BST)

Algorithms
Medium
Apple
70.5K 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 smaller than its right child, which fails if a descendant violates the original ancestor's constraint. For example, a node might be larger than its immediate parent but smaller than the grandparent's left branch limit. My approach involves a recursive helper function that carries valid lower and upper bounds. When we visit the root, the range is negative infinity to positive infinity. For any left child, the new upper bound becomes the current node's value, while the lower bound remains unchanged. Conversely, for a right child, the lower bound updates to the current node's value, keeping the upper bound constant. If a node's value falls outside these exclusive bounds, the tree is invalid. We continue this process until all nodes are verified or a violation is found. This ensures O(N) time complexity as we visit each node exactly once. Regarding Apple's focus on precision, I would also consider integer overflow by using Long integers for bounds if the input allows extreme values, ensuring the solution is robust for production-grade systems.

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

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 54 Apple questions