Medium Data Structures Trees & Binary Trees
How do you validate if a binary tree is a valid BST?
Answer
Use recursion with min/max range validation. Start with range (-infinity, infinity). For each node, check if its value is within the valid range. Recursively validate left subtree with (min, node.value) and right subtree with (node.value, max). If any node violates its range, return false. Alternative: in-order traversal should produce strictly increasing sequence. Both approaches are O(n) time and O(h) space.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Algorithm Developer