-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
59 lines (48 loc) · 1.25 KB
/
solution.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
/*
* @lc app=leetcode id=95 lang=javascript
*
* [95] Unique Binary Search Trees II
*/
class TreeNode<T = any> {
constructor(
public val: T,
public left: TreeNode<T> | null = null,
public right: TreeNode<T> | null = null,
) {}
}
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number} n
* @return {TreeNode[]}
*/
const generateTrees = (n: number): TreeNode[] => {
// * ['72 ms', '79.12 %', '37.9 MB', '66.67 %']
if (n == 0) return [];
const fullArr: number[] = Array.from({ length: n }, (e, i) => i + 1);
const genTrees = (arr: number[], a: number, b: number): (TreeNode | null)[] => {
if (a > b) return [null];
if (a == b) return [new TreeNode(arr[a])];
const result: TreeNode[] = [];
for (let j = a; j <= b; j++) {
const mid = arr[j];
const lefts = genTrees(arr, a, j - 1);
const rights = genTrees(arr, j + 1, b);
for (const l of lefts) {
for (const r of rights) {
result.push(new TreeNode(mid, l, r));
}
}
}
return result;
};
return genTrees(fullArr, 0, n - 1) as TreeNode[];
};
// @lc code=end
export { TreeNode, generateTrees };