Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
if n == 0:
return []
return self.dfs(1, n+1)
def dfs(self, start, end):
if start == end:
return None
result = []
for i in xrange(start, end):
for left in self.dfs(start, i) or [None]:
for right in self.dfs(i+1, end) or [None]:
node = TreeNode(i)
node.left, node.right = left, right
result.append(node)
return result