mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
Update and rename bst_all_subnodes.py to bst_all_subnodes.py
This commit is contained in:
parent
cd5ac00126
commit
eedb6b6863
1 changed files with 1 additions and 3 deletions
|
@ -1,39 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
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:
|
||||
result.append(None)
|
||||
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:
|
||||
root = Node(i, l, r)
|
||||
result.append(root)
|
||||
|
||||
memo[(start, end)] = result
|
||||
return result
|
||||
|
||||
|
||||
def generate_trees(n):
|
||||
|
||||
memo = {}
|
||||
return all_possible_bst(1, n, memo)
|
Loading…
Add table
Add a link
Reference in a new issue