[LeetCode]题解(python):096-Unique Binary Search Trees
题目来源:
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的更多相关文章
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- 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 ...
- 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 ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- 096 Unique Binary Search Trees 不同的二叉查找树
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- 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 ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- [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 ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- Minimum Inversion Number(归并排序)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 什么是xss盲打
什么是xss盲打? 盲打仅仅是一种惯称的说法,就是不知道后台不知道有没有xss存在的情况下,不顾一切的输入xss代码在留言啊,feedback啊之类的地方,尽可能多的尝试xss的语句与语句的存在方式, ...
- android项目的的目录结构
然后我们看一下Helloword的程序目录: 我们可以看到 大致有的文件: 1. MainHelloWorld.java文件 2. R.java文件 3. android.jar文件 4. RES.L ...
- android UI进阶之style和theme的使用
今天来和大家分享一下android中UI设计里面常会用到的style和theme. 首先,style和theme都是资源,android提供了很多这样的默认资源.你可以来使用它们.同时你也可以自己定义 ...
- JavaSE思维导图(五)
- MYSQL知识点
1.MYSQL为了可移植性,使用"--"做为注释,使用"/*!*/"表示可执行的注释.
- SQLserver数据库操作帮助类SqlHelper
1 SqlHelper源码 using System; using System.Data; using System.Xml; using System.Data.SqlClient; using ...
- 原生js判断某个元素是否有指定的class名的几种方法
[注意]以下方法只对class只有一个值的情况下操作 ************************************************************* 结构部分: <d ...
- 单选,复选操作div,显示隐藏
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- qt检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】
#include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...