Skip to content

Commit b8027fe

Browse files
committed
Add solution #98
1 parent 1460488 commit b8027fe

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
83|[Remove Duplicates from Sorted List](./0083-remove-duplicates-from-sorted-list.js)|Easy|
4545
88|[Merge Sorted Array](./0088-merge-sorted-array.js)|Easy|
4646
94|[Binary Tree Inorder Traversal](./0094-binary-tree-inorder-traversal.js)|Easy|
47+
98|[Validate Binary Search Tree](./0098-validate-binary-search-tree.js)|Medium|
4748
101|[Symmetric Tree](./0101-symmetric-tree.js)|Easy|
4849
102|[Binary Tree Level Order Traversal](./0102-binary-tree-level-order-traversal.js)|Medium|
4950
104|[Maximum Depth of Binary Tree](./0104-maximum-depth-of-binary-tree.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 98. Validate Binary Search Tree
3+
* https://leetcode.com/problems/validate-binary-search-tree/
4+
* Difficulty: Medium
5+
*
6+
* Given the root of a binary tree, determine if it is a valid binary search tree (BST).
7+
*
8+
* A valid BST is defined as follows:
9+
* - The left subtree of a node contains only nodes with keys less than the node's key.
10+
* - The right subtree of a node contains only nodes with keys greater than the node's key.
11+
* - Both the left and right subtrees must also be binary search trees.
12+
*/
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* function TreeNode(val, left, right) {
17+
* this.val = (val===undefined ? 0 : val)
18+
* this.left = (left===undefined ? null : left)
19+
* this.right = (right===undefined ? null : right)
20+
* }
21+
*/
22+
/**
23+
* @param {TreeNode} root
24+
* @return {boolean}
25+
*/
26+
var isValidBST = function(root) {
27+
return traverse(root, null, null);
28+
};
29+
30+
function traverse(root, min, max) {
31+
if (!root) {
32+
return true;
33+
}
34+
35+
if ((min !== null && root.val <= min) || (max !== null && root.val >= max)) {
36+
return false;
37+
}
38+
39+
return traverse(root.left, min, root.val) && traverse(root.right, root.val, max);
40+
}

0 commit comments

Comments
 (0)