Skip to content

Commit 98fa450

Browse files
committed
Validate Binary Search Tree / 중급
1 parent 94cfe6d commit 98fa450

File tree

1 file changed

+24
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)