题目来源:

  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. mysql 权限控制具体解释

    概述 mysql权限控制在不同的上下文和不同的操作水平上都能够进行控制,他们包括例如以下几个 ** 管理权限能够同意用户管理mysql server的操作. 这些权限控制是全局的,不是针对某个特定的数 ...

  2. SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled

    SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled 今天是2013-09-17,在今天学习sql ...

  3. iptables 配置

    #查看iptables现有规则 iptables -L -n #先允许所有,不然可能悲剧 iptables -P INPUT ACCEPT #清除所有默认规则 iptables -F #清除自定义规则 ...

  4. samba服务器的安装及配置

    安装前首先查看服务器是否已经安装samba服务器 [root@bogon home]# rpm -qa|grep samba system-config-samba-docs-1.0.9-1.el6. ...

  5. 使用 UML 进行业务建模:理解业务用例与系统用例的相似和不同之处

    使用 UML 进行业务建模:理解业务用例与系统用例的相似和不同之处   作者:Arthur V. English 出处:IBM   本文内容包括: 背景 业务用例模型与系统用例模型有什么相似之处? 业 ...

  6. 张孝祥Java高新技术汇总

    一.自动装箱和拆箱: 在Java中有8种基本数据类型:byte,short,int,long,float,double,char,boolean.而基本数据类型不是对象,这时人们给他们定义了包装类,使 ...

  7. 【转】Virtualbox虚拟机配置安装CentOS 6.5图文教程

    http://www.111cn.net/sys/CentOS/61709.htm 什么是Virtualbox? VirtualBox 是一款开源虚拟机软件(注:跟vmware差不多).Virtual ...

  8. 一道Python练习题

    有关字符串与编码的,转自廖雪峰Python教程. 小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',只保留小数点后1位: # -*- co ...

  9. Spring jdbctemplate学习笔记

    /*List<?> config = getDB(" select t.datavalue from sys_config t where t.configid = '15' & ...

  10. 2015 8月之后"云计算"学习计划

    1. 自己在家搭建openstack,使用RDO搭建自己的openstack环境,不必源码方式搭建,只要搭建起来就好,越快越好 --以RDO方式,搭建一个all-in-one的主机,只需要租一台虚拟机 ...