0098. Validate BST
Medium | Binary Search Tree + Recursion / Stack | 28 ms (94.97%), 17.7 MB (95.43%)
Last updated
Was this helpful?
Medium | Binary Search Tree + Recursion / Stack | 28 ms (94.97%), 17.7 MB (95.43%)
Last updated
Was this helpful?
Source: GitHub:
Given the root
of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
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.
For visiting the left-child node, we update the upper bound by the current node's value and keep the lower bound (All child nodes in the left-subtree should not have values greater than their parent.)
For visiting the right-child node, we update the lower bound by the current node's value and keep the upper bound (All child nodes in the right-subtree should not have values less than their parent.)