题目来源:

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


题意分析:

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


题目思路:

  这是动态规划的题目。选定了第k个为根节点,那么所有的可能就是ans[k-1] * ans[n -k]其中,ans[i]代表i整数i一共有ans[i]种可能。


代码(python):

  

class Solution(object):
def numTrees(self, n):
"""
:type n: int
:rtype: int
"""
ans = [0 for i in range(n + 1)]
ans[0],ans[1] = 1,1
for i in range(2,n+1):
for j in range(i/2):
ans[i] += ans[j]*ans[i - 1 - j]
ans[i] *= 2
if i % 2 == 1:
ans[i] += ans[i/2]*ans[i/2]
return ans[n]

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

  1. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  2. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  3. Java for LeetCode 096 Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  4. LeetCode(95) Unique Binary Search Trees II

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

  5. 096 Unique Binary Search Trees 不同的二叉查找树

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

  6. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

  7. 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 ...

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

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

  9. [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

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

  10. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. Trailing Zeroes (III)(lightoj 二分好题)

    1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit:  ...

  2. 【翻译】在Ext JS 5种使用ViewControllers

    原文:Using ViewControllers in Ext JS 5 简单介绍 在Ext JS 5中,在应用程序架构方面提供了一些令人兴奋的改进,如加入了ViewModels.MVVM以及view ...

  3. mac 版本号控制工具SmartSVN7.5.4(破解版)

    SmartSVN7.5.4和破解工具,下载地址:http://download.csdn.net/detail/pearlhuzhu/7407319 操作步骤: 1.在MAC上选中smartsvn-m ...

  4. javaScript操作select

    注意:Option中的O是要大写的,不然语法报错 1.动态创建select       function createSelect(){ var mySelect = document.createE ...

  5. Hibernate session.saveOrUpdate()方法

    saveOrUpdate()方法同时包含了save()与update()方法的功能, 如果传入的参数是临时对象,就调用save()方法: 如果传入的参数是游离对象,就调用update()方法: 如果传 ...

  6. cocostudio导出plist文件

    今天在用Armature类时用到cocostudio导出文件,由于美术的原因他使用的是中文命名法(这你敢相信),后面在导入程序中跟了下代码发现是解析plist文件有误,我就来比较正常功能文件和有错文件 ...

  7. SGU 187.Twist and whirl - want to cheat( splay )

    维护一个支持翻转次数M的长度N的序列..最后输出序列.1<=N<=130000, 1<=M<=2000 splay裸题... ------------------------- ...

  8. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  9. memcached look status

    $ STAT pid STAT STAT STAT version STAT pointer_size STAT rusage_user 7.054927 STAT rusage_system 14. ...

  10. python parse命令行参数

    #!/usr/bin/env python import sys def main(argv): for arg in argv: print arg if __name__ == '__main__ ...