-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution-fancy-inorder.ts
42 lines (34 loc) · 833 Bytes
/
solution-fancy-inorder.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
/*
* @lc app=leetcode id=98 lang=javascript
*
* [98] Validate Binary Search Tree
*/
type MaybeTreeNode = TreeNode | null;
type TreeNode = {
val: number;
left: MaybeTreeNode;
right: MaybeTreeNode;
};
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
const isValidBST = (root: MaybeTreeNode): boolean => {
// * ['208 ms', '5.46 %', '53 MB', '10.26 %']
if (root === null) return true;
return inOrder(root).every((e, i, a) => i === 0 || a[i - 1] < e);
};
const inOrder = (root: MaybeTreeNode): number[] => {
if (root === null) return [];
return [...inOrder(root.left), root.val, ...inOrder(root.right)];
};
// @lc code=end
export { isValidBST };