Skip to content

Commit cf47e7c

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 95 and 96
1 parent 1c06810 commit cf47e7c

5 files changed

+103
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
122122
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
123123
- [91 Decode Ways](https://leetcode.com/problems/decode-ways/description/)
124124
- [92 Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/description/)
125+
- [95 Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/description/)
126+
- [96 Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/description/)
125127
- [97 Interleaving String](https://leetcode.com/problems/interleaving-string/description/)
126128
- [98 Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/)
127129
- [100 Same Tree](https://leetcode.com/problems/same-tree/description/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List, Optional, Tuple
2+
3+
from awesome_python_leetcode.tree import TreeNode
4+
5+
6+
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
9+
def generateTrees(self, n: int) -> List[Optional[TreeNode]]:
10+
"""
11+
Given an integer n, return all the structurally unique BST's
12+
(binary search trees), which has exactly n nodes of unique values from 1 to n.
13+
Return the answer in any order.
14+
"""
15+
dp = {}
16+
17+
def dfs(remain: Tuple[int]):
18+
if not remain:
19+
return [None]
20+
if remain in dp:
21+
return dp[remain]
22+
trees = []
23+
for i in range(len(remain)):
24+
left = dfs(remain[:i])
25+
right = dfs(remain[i + 1 :])
26+
for left_ in left:
27+
for right_ in right:
28+
node = TreeNode(val=remain[i])
29+
node.left = left_
30+
node.right = right_
31+
trees.append(node)
32+
dp[remain] = trees
33+
return trees
34+
35+
return dfs(tuple([i for i in range(1, n + 1)]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def numTrees(self, n: int) -> int:
5+
"""
6+
Given an integer n, return the number of structurally unique BST's
7+
(binary search trees) which has exactly n nodes of unique values from 1 to n.
8+
"""
9+
dp = [0 for _ in range(n)]
10+
dp[0] = 1
11+
for i in range(1, n):
12+
for k in range((i + 1) // 2):
13+
if k == 0:
14+
dp[i] += dp[i - k - 1]
15+
else:
16+
dp[i] += dp[i - k - 1] * dp[k - 1]
17+
dp[i] *= 2
18+
if (i + 1) % 2:
19+
dp[i] += dp[i - ((i + 1) // 2) - 1] ** 2
20+
return dp[-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._95_unique_binary_search_trees_II import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["n", "expected"],
11+
argvalues=[
12+
(
13+
3,
14+
[
15+
[1, None, 2, None, 3],
16+
[1, None, 3, 2],
17+
[2, 1, 3],
18+
[3, 1, None, None, 2],
19+
[3, 2, None, 1],
20+
],
21+
),
22+
(1, [[1]]),
23+
],
24+
)
25+
def test_func(n: int, expected: List[List[int]]):
26+
"""Tests the solution of a LeetCode problem."""
27+
expected = [TreeNode.build(tree) for tree in expected]
28+
bsts = Solution().generateTrees(n)
29+
assert all(actual == expected for actual, expected in zip(bsts, expected))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._96_unique_binary_search_trees import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["n", "expected"],
8+
argvalues=[
9+
(3, 5),
10+
(1, 1),
11+
(6, 132),
12+
],
13+
)
14+
def test_func(n: int, expected: int):
15+
"""Tests the solution of a LeetCode problem."""
16+
num_bsts = Solution().numTrees(n)
17+
assert num_bsts == expected

0 commit comments

Comments
 (0)