# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
#@return a list of tree node with value range from start to end
def TreeGen(self, start,end):
if start>end:
return [None]
rst=[]
for rVal in range(start, end+1):
leftlist=self.TreeGen(start,rVal-1)
rightlist=self.TreeGen(rVal+1,end)
for i in leftlist:
for j in rightlist:
root=TreeNode(rVal)
root.left=i
root.right=j
rst.append(root)
return rst
# @return a list of tree node
def generateTrees(self, n):
return self.TreeGen(1,n)
Friday, September 12, 2014
Leetcode: Unique Binary Search Trees II @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment