题目来源:

  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]

最新文章

  1. 关于click和submit的笔记
  2. Port Hacking
  3. python报错:SyntaxError: Non-ASCII character '\xe5'的解决方法
  4. 在调用“Fill”前,SelectCommand 属性尚未初始化
  5. winform清空DataGridView中的数据 分类: DataGridView 2014-05-19 20:56 180人阅读 评论(0) 收藏
  6. C# 面向对象 , 类与对象
  7. #include <boost/function.hpp>
  8. 推荐书目 - C++学习资料
  9. [Android学习笔记]LayoutParams的使用
  10. C#对html的操作
  11. 2017-3-10 SQL server 数据库 T--SQL语句
  12. 基于.netstandard的权限控制组件
  13. 项目管理软件之争,禅道和JIRA大对比
  14. web基础系列(五)---https是如何实现安全通信的
  15. JavaScript系列----数据类型以及传值和传引用
  16. codeforces611C
  17. 百度外卖接口调试 C#版
  18. [翻译] UCZProgressView
  19. Linux_Apache 安装
  20. PAT——1054. 求平均值

热门文章

  1. Ubuntu小私房(3)--Uubutnu启动美化大变身
  2. 金山卫士开源软件之旅(十) KSafeMainproject的分析 1
  3. android SDK和ADT的更新
  4. c++ build options(important)
  5. [C#参考]主线程和子线程之间的参数传递
  6. find: missing argument to `-exec'
  7. (Problem 92)Square digit chains
  8. linux 怎么查找oracle11g的安装目录
  9. qt5集成libcurl实现tftp和ftp的方法一:搭建环境(五篇文章)
  10. 简单测试运行时类信息(RTTI),附详细例子