A client-side cache stores records in a binary search tree keyed by timestamp. After a buggy migration, you need a sanity check. Write isValidBST(root), which returns true if the tree is a valid binary search tree:
Each node is { value, left, right } with numeric values; left/right are nodes or null. An empty tree (null) is valid.
// 8
// / \
// 3 10
isValidBST(tree1); // true
// 8
// / \
// 3 10
// \
// 9 ← 9 is right of 3 (fine locally) but inside 8's LEFT subtree, and 9 > 8
isValidBST(tree2); // false
// 5
// /
// 5 ← duplicates are not allowed
isValidBST(tree3); // false
Spotted a mistake or a rights concern? Report an issue