-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.ts
45 lines (37 loc) · 1.24 KB
/
res.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
/**
* 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)
* }
* }
*/
function getTrees(nums: number[]): Array<TreeNode | null> {
if (!nums.length) {
return [null];
}
const result = [];
for (let i = 0; i < nums.length; i++) {
const currentVal = nums[i];
const leftTrees = getTrees(nums.slice(0, i));
const rightTrees = getTrees(nums.slice(i + 1));
for (let j = 0; j < leftTrees.length; j++) {
for (let k = 0; k < rightTrees.length; k++) {
const rootNode = new TreeNode(currentVal);
rootNode.left = leftTrees[j];
rootNode.right = rightTrees[k];
result.push(rootNode);
}
}
}
return result.filter(Boolean);
}
function generateTrees(n: number): Array<TreeNode | null> {
const nums = new Array(n).fill(0).map((_, i) => i + 1);
return getTrees(nums);
};