-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path95.不同的二叉搜索树-ii.py
More file actions
61 lines (59 loc) · 1.53 KB
/
95.不同的二叉搜索树-ii.py
File metadata and controls
61 lines (59 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
#
# @lc app=leetcode.cn id=95 lang=python
#
# [95] 不同的二叉搜索树 II
#
# https://leetcode-cn.com/problems/unique-binary-search-trees-ii/description/
#
# algorithms
# Medium (59.06%)
# Likes: 190
# Dislikes: 0
# Total Accepted: 11.3K
# Total Submissions: 19.1K
# Testcase Example: '3'
#
# 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
#
# 示例:
#
# 输入: 3
# 输出:
# [
# [1,null,3,2],
# [3,2,null,1],
# [3,1,null,null,2],
# [2,1,3],
# [1,null,2,null,3]
# ]
# 解释:
# 以上的输出对应以下 5 种不同结构的二叉搜索树:
#
# 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:
def generateTrees(self, n):
if n==0:return []
def generate(first, last):
trees = []
for root in range(first, last+1):
for left in generate(first, root-1):
for right in generate(root+1, last):
node = TreeNode(root)
node.left = left
node.right = right
trees += node
return trees or [None] # 0 or 42 返回42 而不是返回布尔值
return generate(1, n)