forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0095.py
31 lines (26 loc) · 841 Bytes
/
0095.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def generateTrees(self, n: int) -> List[TreeNode]:
if n == 0:
return []
return self.helper(1, n)
def helper(self, min: int, max: int) -> List[TreeNode]:
ans = []
if min > max:
ans.append(None)
return ans
for i in range(min, max + 1):
leftTree = self.helper(min, i - 1)
rightTree = self.helper(i + 1, max)
for left in leftTree:
for right in rightTree:
root = TreeNode(i)
root.left = left
root.right = right
ans.append(root)
return ans