[LeetCode]题解(python):096-Unique Binary Search Trees
2024-10-09 04:32:35
题目来源:
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]
最新文章
- 关于click和submit的笔记
- Port Hacking
- python报错:SyntaxError: Non-ASCII character '\xe5'的解决方法
- 在调用“Fill”前,SelectCommand 属性尚未初始化
- winform清空DataGridView中的数据 分类: DataGridView 2014-05-19 20:56 180人阅读 评论(0) 收藏
- C# 面向对象 , 类与对象
- #include <;boost/function.hpp>;
- 推荐书目 - C++学习资料
- [Android学习笔记]LayoutParams的使用
- C#对html的操作
- 2017-3-10 SQL server 数据库 T--SQL语句
- 基于.netstandard的权限控制组件
- 项目管理软件之争,禅道和JIRA大对比
- web基础系列(五)---https是如何实现安全通信的
- JavaScript系列----数据类型以及传值和传引用
- codeforces611C
- 百度外卖接口调试 C#版
- [翻译] UCZProgressView
- Linux_Apache 安装
- PAT——1054. 求平均值
热门文章
- Ubuntu小私房(3)--Uubutnu启动美化大变身
- 金山卫士开源软件之旅(十) KSafeMainproject的分析 1
- android SDK和ADT的更新
- c++ build options(important)
- [C#参考]主线程和子线程之间的参数传递
- find: missing argument to `-exec&#39;
- (Problem 92)Square digit chains
- linux 怎么查找oracle11g的安装目录
- qt5集成libcurl实现tftp和ftp的方法一:搭建环境(五篇文章)
- 简单测试运行时类信息(RTTI),附详细例子