[LeetCode]Unique Binary Search Trees II 唯一二叉树2
Unique Binary Search Trees II 唯一二叉树2
class Solution(object):
def generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
def helper(start, end):
res = []
if start > end:
res.append(None)
return res
for i in range(start, end+1):
left_child = helper(start, i-1)
right_child = helper(i+1, end)
for left in left_child:
for right in right_child:
root = TreeNode(i)
root.left, root.right = left, right
res.append(root)
return res
if n < 1:
return []
return helper(1, n)
关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法

浙公网安备 33010602011771号