Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74ef4e4

Browse files
update: 95
1 parent 0df24de commit 74ef4e4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6666
| 91 | [Decode Ways](https://leetcode.com/problems/decode-ways/) | [JavaScript](./src/decode-ways/res.js) | Medium |
6767
| 93 | [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) | [JavaScript](./src/restore-ip-addresses/res.js) | Medium |
6868
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [TypeScript](./src/binary-tree-inorder-traversal/res.ts) | Easy |
69+
| 95 | [unique-binary-search-trees-ii](https://leetcode.com/problems/unique-binary-search-trees-ii/) | [TypeScript](./src/unique-binary-search-trees-ii/res.ts) | Medium |
6970
| 96 | [unique-binary-search-trees](https://leetcode.com/problems/unique-binary-search-trees/) | [TypeScript](./src/unique-binary-search-trees/res.ts) | Medium |
7071
| 97 | [interleaving-string](https://leetcode.com/problems/interleaving-string/) | [TypeScript](./src/interleaving-string/res.ts) | Medium |
7172
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [JavaScript](./src/validate-binary-search-tree/res.js) | Medium |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function getTrees(nums: number[]): Array<TreeNode | null> {
16+
if (!nums.length) {
17+
return [null];
18+
}
19+
20+
const result = [];
21+
22+
for (let i = 0; i < nums.length; i++) {
23+
const currentVal = nums[i];
24+
25+
const leftTrees = getTrees(nums.slice(0, i));
26+
const rightTrees = getTrees(nums.slice(i + 1));
27+
28+
for (let j = 0; j < leftTrees.length; j++) {
29+
for (let k = 0; k < rightTrees.length; k++) {
30+
const rootNode = new TreeNode(currentVal);
31+
rootNode.left = leftTrees[j];
32+
rootNode.right = rightTrees[k];
33+
result.push(rootNode);
34+
}
35+
}
36+
}
37+
38+
return result.filter(Boolean);
39+
}
40+
41+
function generateTrees(n: number): Array<TreeNode | null> {
42+
const nums = new Array(n).fill(0).map((_, i) => i + 1);
43+
44+
return getTrees(nums);
45+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /