Skip to content

Commit 0449874

Browse files
committed
validate-binary-search-tree solved
1 parent 036de93 commit 0449874

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

validate-binary-search-tree/hsskey.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
const dfs = (node, left = null, right = null) => {
15+
if(!node) {
16+
return true
17+
}
18+
19+
if((left !== null && node.val <= left) || (right !== null && node.val >= right)) {
20+
return false
21+
}
22+
23+
return dfs(node.left, left, node.val) && dfs(node.right, node.val, right)
24+
}
25+
26+
return dfs(root)
27+
};

0 commit comments

Comments
 (0)