Mid From PDF Coding C# Coding Interview

Check if a binary tree is a valid BST (with recursion)?

bool IsValidBST(TreeNode root) {

return Validate(root, null, null);
}

Follow on:

bool Validate(TreeNode node, int? min, int? max) {

if (node == null) return true;
if ((min != null && node.val <= min) || (max != null && node.val
>= max)) return false;
return Validate(node.left, min, node.val) &&

Validate(node.right, node.val, max);

}

Explanation:

Pass down min and max bounds for subtree values; node must be in (min, max) range.

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details