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?
Short answer: 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. min, int? max) {… if (node == null) return…… true; if ((min != null && node.val <= min) || (max != null…
Explain a bit more
&& 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.
Real-world example (ShopNest)
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png