Hi, here’s your problem today. This problem was recently asked by Amazon:
Given a number n, generate all binary search trees that can be constructed with nodes 1 to n.
Here’s some code to start with:
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __str__(self):
result = str(self.value)
if self.left:
result = result + str(self.left)
if self.right:
result = result + str(self.right)
return result
def generate_bst(n):
# Fill this in.
for tree in generate_bst(3):
print tree
# Pre-order traversals of binary trees from 1 to n.
# 123
# 132
# 213
# 312
# 321
# 1 1 2 3 3
# \ \ / \ / /
# 2 3 1 3 1 2
# \ / \ /
# 3 2 2 1