Mid Coding

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