js-practice/src/tree/validate-binary-search-tree.js
2020-05-05 18:25:52 +08:00

17 lines
461 B
JavaScript

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
export const isValidBST = function (root, lower = -Infinity, upper = Infinity) {
if (root === null) return true
if (root.val <= lower || root.val >= upper) return false
return isValidBST(root.left, lower, root.val) && isValidBST(root.right, root.val, upper)
}