Skip to content

Commit 18f4003

Browse files
committed
validate-binary-search-tree solution
1 parent 1d8c8ae commit 18f4003

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {boolean}
12+
*/
13+
var isValidBST = function(root) {
14+
function dfs(node, low, high) {
15+
if(!node) return true;
16+
17+
if(!(low < node.val && node.val < high)) return false;
18+
19+
return dfs(node.left, low, node.val) && dfs(node.right, node.val, high);
20+
}
21+
22+
return dfs(root, -Infinity, Infinity);
23+
};

0 commit comments

Comments
 (0)