[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 ...
随机推荐
- [置顶] Guava学习之Splitter
Splitter:在Guava官方的解释为:Extracts non-overlapping substrings from an input string, typically by recogni ...
- Maven真——聚合和继承(于)
依赖管理 我们谈论继承一个dependencies因素,我们非常easy这个特性被认为是适用于accout-parent于. 子模块account-email和account-persist同一时候依 ...
- Android Navigation Drawer(导航抽屉)
Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建 Navigation ...
- linux 自旋锁
一.概述: 自旋锁是SMP架构中的一种low-level的同步机制.当线程A想要获取一把自旋锁而该锁又被其它线程锁持有时,线程A会在一个循环中自旋以检测锁是不是已经可用了.对于自选锁需要注意: 由于自 ...
- java中log4j的使用体验
log4j相信大部分java开发者都已经很熟悉了,在此记录下自己的使用过程. 一.文件准备: 1.log4j.jar(我这里使用的版本是log4j-1.2.17.jar,在附件中提供下载) 2.log ...
- google base之LockImpl
为了兼容不同的平台,这个类采用了impl模式,win平台通过CRITICAL_SECTION, 这样的话还是相对比较简单,具体就不详解了,不过不得不说boost的实现方式就要复杂到哪里去了,当然,好处 ...
- Excel Sheet Row Numbers
Given the sequence S1 = {a,b,c,d,…,x,y,z,aa,ab,ac…. } and given that this sequence corresponds (term ...
- J - 今年暑假不AC
J - 今年暑假不AC Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- Excel VLOOKUP等使用记录
1.LEFT 函数:LEFT(G2,ROW(INDIRECT("76:"&LEN(G2)))) 截取G2单元格从开头,SIZE=76的部分字符串 2.RIGHT 函数:RI ...
- FileNameExtensionFilter文件过滤
package com.soft.test; import javax.swing.*; import javax.swing.filechooser.*; import java.awt.event ...