Leetcode: Given a binary tree, determine if it is a valid binary search tree (BST).

The left subtree of a node contains only nodes with keys less than the node's key.

The right subtree of a node contains only nodes with keys greater than the node's key.

Both the left and right subtrees must also be binary search trees.

Valid BST

2
/ \
1 3

5
/ \
3 10
/ \
1 4

Invalid BST

3
/ \
1 2

6
/ \
3 10
/ \
1 7

At first pass, it may seem reasonable to have a check where if the condition left.val < root.val < right.val satisfies then a node is valid and same check can be done for each node. But this condition failes in certain cases, especially in 2nd example of Invalid BST. The trick to solve the problem is one need to maintain MIN & MAX counter from the root and as tree is traversed update the counter.