mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update README.md
This commit is contained in:
parent
cf170e3d57
commit
cd5ac00126
@ -154,6 +154,47 @@ def climb_stairs_memoization(n: int) -> int:
|
|||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
* another good examples is calculating all possible subnodes in a tree:
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Node:
|
||||||
|
def __init__(self, val=0, left=None, right=None):
|
||||||
|
self.val = val
|
||||||
|
self.left = left
|
||||||
|
self.right = right
|
||||||
|
|
||||||
|
|
||||||
|
def all_possible_bst(start, end, memo):
|
||||||
|
|
||||||
|
result = []
|
||||||
|
if start > end:
|
||||||
|
return result
|
||||||
|
|
||||||
|
if (start, end) in memo:
|
||||||
|
return memo[(start, end)]
|
||||||
|
|
||||||
|
for i in range(start, end + 1):
|
||||||
|
left = all_possible_bst(start, i - 1, memo)
|
||||||
|
right = all_possible_bst(i + 1, end, memo)
|
||||||
|
|
||||||
|
for l in left:
|
||||||
|
for r in right:
|
||||||
|
result.append(Node(i, l, r))
|
||||||
|
|
||||||
|
memo[(start, end)] = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def generate_trees(n):
|
||||||
|
|
||||||
|
memo = {}
|
||||||
|
return all_possible_bst(1, n, memo)
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
### time complexity
|
### time complexity
|
||||||
@ -208,3 +249,5 @@ def backtrack(candidate):
|
|||||||
remove(next_candidate)
|
remove(next_candidate)
|
||||||
````
|
````
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user