题目来源:

  https://leetcode.com/problems/unique-binary-search-trees-ii/


题意分析:

  给一个整数,返回所有中序遍历是1到n的树。


题目思路:

  这可以用递归的思想。首先确定根节点,如果k是根节点,那么1-k-1是左子树,而k+1-n为右子树。


代码(python):

  

# 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 solve(begin,end):
if begin > end:
return [None]
i = begin
ans = []
while i <= end:
tmp1,tmp2 = solve(begin, i - 1),solve(i + 1,end)
for j in tmp1:
for k in tmp2:
tmp = TreeNode(i)
tmp.left = j
tmp.right = k
ans.append(tmp)
i += 1
return ans
return solve(1,n)

[LeetCode]题解(python):095-Unique Binary Search Trees II的更多相关文章

  1. Java for LeetCode 095 Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  2. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  3. 095 Unique Binary Search Trees II 不同的二叉查找树 II

    给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树:   1         3     3      2      1    ...

  4. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  5. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  6. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  7. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  9. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  10. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

随机推荐

  1. 随心所欲~我也做个集合遍历器吧(自己的foreach,委托的威力)

    感觉微软在面向对象三大原则中,封装性运用的最为突出,它会将一些复杂的算法,结构,功能代码进行封装,让程序员在使用时十分得心应手,如关键字里的foreach和labmda表达式里的Foreach等等,今 ...

  2. 汉诺塔III 汉诺塔IV 汉诺塔V (规律)

    汉诺塔III Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. win7环境下安装MongoDB

    1.从http://www.mongodb.org/downloads获取,下载适合windows版本的mongodb,注意32位和64位的区别2.将下载的zip版本,解压到D:/mongodb3.创 ...

  4. Cookie已经过时,细看Facebook, Google, Apple如何追踪用户

    http://www.infoq.com/cn/news/2014/10/cookie-facebook-google-apple 链接地址 Cookie,有时也用其复数形式Cookies,指某些网站 ...

  5. ORA-31626:作业不存在 ORA-31633:无法创建主表"XXX.SYS_IMPORT_FULL_05"

    错误代码: ORA-31626:作业不存在 ORA-31633:无法创建主表"XXX.SYS_IMPORT_FULL_05" ORA-06512:在"SYS.DBMS_S ...

  6. IM,游戏服务端 tcp 框架整理

    Mina: Mina(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应 ...

  7. 《Pointers On C》读书笔记(第三章 数据)

    1.在C语言中,仅有4种基本数据类型:整型.浮点型.指针和聚合类型(如数组和结构等). 整型家族包括字符.短整型.整型和长整型,它们都分为有符号和无符号两种. 标准规定整型值相互之间大小的规则:长整型 ...

  8. linux服务器在运行210天左右宕机

    减小字体 增大字体 作者:错新网  来源:www.cuoxin.com  发布时间:2014-2-25 19:21:32 错新网讯   最近几天,一批linux线上的服务器接连宕机,当时以为是硬件问题 ...

  9. JavaEE Tutorials (14) - 用实体图创建获取计划

    14.1实体图基础185 14.1.1默认实体图186 14.1.2在持久化操作中使用实体图18614.2使用命名实体图187 14.2.1对实体类应用命名实体图注解187 14.2.2从命名实体图获 ...

  10. HDU 1012 u Calculate e

    题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...