-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLeetCode95unique-binary-search-trees-ii.py
More file actions
executable file
·49 lines (41 loc) · 1.2 KB
/
LeetCode95unique-binary-search-trees-ii.py
File metadata and controls
executable file
·49 lines (41 loc) · 1.2 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/10/14 10:18 AM
# @Author : Slade
# @File : LeetCode95unique-binary-search-trees-ii.py
# 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 []
def get_tree(start, end):
ans = []
if start > end:
ans.append(None)
return ans
if start == end:
ans.append(TreeNode(start))
return ans
for i in range(start, end + 1):
left = get_tree(start, i - 1)
right = get_tree(i + 1, end)
for l in left:
for r in right:
root = TreeNode(i)
root.left = l
root.right = r
ans.append(root)
return ans
return get_tree(1, n)
if __name__ == '__main__':
s = Solution()
print (s.generateTrees(0))