Skip to content

Commit 1ac9481

Browse files
authored
feat: validate-binary-search-tree solution
1 parent f019efe commit 1ac9481

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isValidBST(root: TreeNode | null): boolean {
16+
if (!root) return true;
17+
18+
function isValid(node: TreeNode | null, min: number, max: number): boolean {
19+
if (!node) return true;
20+
if (node.val <= min || node.val >= max) return false;
21+
22+
return isValid(node.left, min, node.val) &&
23+
isValid(node.right, node.val, max)
24+
}
25+
26+
// ์ดˆ๊ธฐ ํ˜ธ์ถœ (๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ฒ”์œ„๋Š” ๋ฌดํ•œ๋Œ€)
27+
return isValid(root, -Infinity, Infinity)
28+
}

0 commit comments

Comments
ย (0)