-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.validate-binary-search-tree.ts
55 lines (46 loc) · 1.78 KB
/
98.validate-binary-search-tree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode id=98 lang=typescript
*
* [98] Validate Binary Search Tree
*/
// @lc code=start
// Definition for a binary tree node.
// class TreeNode {
// val: number;
// left: TreeNode | null;
// right: TreeNode | null;
// constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
// this.val = val === undefined ? 0 : val;
// this.left = left === undefined ? null : left;
// this.right = right === undefined ? null : right;
// }
// }
// this is a recursive problem
// when checking a node, we alsways look if the left node is smaller than the current node
// we also check if the right node is bigger than the current node
// if this is fulfilled, we check if the left and right nodes are valid BSTs
// so we pass them to the function again.
function isValidBST(root: TreeNode | null, min: number = Number.MIN_SAFE_INTEGER, max: number = Number.MAX_SAFE_INTEGER): boolean {
// if the current node has no value, we consider it valid
if (root === null) {
return true;
}
// if the current value is smaller than the minimum allowed, the tree is not balanced
if (root.val <= min) {
return false;
}
// if the current value is bigger than the max, the tree is not balanced
if (root.val >= max) {
return false;
}
// check if the left side of the current node is valid. The left node
// is the new root. the minimum is always the max min. The maximum has to be the
// previous nodes value during each step
const leftValid = isValidBST(root.left, min, root.val);
// check if the right side of the current node is valid. The right node
// is the new root. There is no maximum. The minimum has to be the
// previous nodes value during each step
const rightValid = isValidBST(root.right, root.val, max);
return leftValid && rightValid;
}
// @lc code=end