-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueBinarySearchTrees.ts
74 lines (64 loc) · 1.65 KB
/
UniqueBinarySearchTrees.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Source : https://leetcode.com/problems/unique-binary-search-trees/
// Author : squxq
// Date : 2023-11-06
/*****************************************************************************************************
*
* Given an integer n, return the number of structurally unique BST's (binary search trees) which has
* exactly n nodes of unique values from 1 to n.
*
* Example 1:
*
* Input: n = 3
* Output: 5
*
* Example 2:
*
* Input: n = 1
* Output: 1
*
* Constraints:
*
* 1 <= n <= 19
******************************************************************************************************/
/**
* number -> number
* given an integer, n, return the number of structurally unique BST's (binary search trees) which has
* exactly n nodes of unique values from 1 to n
* Stub:
function numTrees(n: number): number {return 0}
* Tests:
* I: n = 3 -> O: 5
* I: n = 1 -> O: 1
* Template:
function numTrees(n: number): number {
return (... (n))
}
* CONSTRAINTS:
* - 1 <= n <= 19
*/
export function numTrees(n: number): number {
return factorial(2 * n) / (factorial(n + 1) * factorial(n));
}
/**
* number -> number
* given an integer, k, return k! (factorial of k)
* NOTE: the factorial of n is the product of all integers less than or equal to k
* ASSUME: 1 <= k <= 19
* Stub:
function factorial(k: number): number {return 0}
* Tests:
* I: k = 1 -> O: 1
* I: k = 8 -> O: 40320
* I: k = 19 -> O: 121645100408832000
* Template:
functoin factorial(k: number): number {
return (... (k))
}
*/
function factorial(k: number): number {
let ans: number = 1;
for (let i: number = 2; i <= k; i++) {
ans = ans * i;
}
return ans;
}