原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/

题意:

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

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

解题思路:这题从数学上讲,其实是卡特兰数。不过我们显然不从数学上来解决这个问题。这题是求二叉树的棵数。这里有个解题技巧:一般来说求数量,要首先想到使用动态规划(dp),而如果是像下一题的要求,不只是数量,还要把所有的树都枚举出来,就要使用dfs(深度优先搜索)来遍历决策树了。

     那么这道题是使用动态规划来解决的。那么如何去求这个问题的状态转移方程呢?其实大部分动态规划的难点都是求状态转移方程。n=0时,为空树,那么dp[0]=1; n=1时,显然也是1,dp[1]=1;n=2时,dp[2]=2; 对于n>2时,dp[n]=dp[0]*dp[n-1]+dp[1]*dp[n-2]+......+dp[n-1]*dp[0];这不就是卡特兰数的定义吗?编程很容易实现。

代码:

class Solution:
# @return an integer
def numTrees(self, n):
dp = [1, 1, 2]
if n <= 2:
return dp[n]
else:
dp += [0 for i in range(n-2)]
for i in range(3, n + 1):
for j in range(1, i+1):
dp[i] += dp[j-1] * dp[i-j]
return dp[n]

[leetcode]Unique Binary Search Trees @ Python的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  4. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. LeetCode - Unique Binary Search Trees II

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

  6. Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  7. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  8. Leetcode Unique Binary Search Trees

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

  9. LeetCode: Unique Binary Search Trees [095]

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

随机推荐

  1. IP基础知识与分配实现

    一.IP寻址 1.划分网络ID和主机ID的最初方案是使用地址分类. 2.A类:0.0.0.0-127.255.255.255 B类:128.0.0.0-191.255.255.255 C类:192.0 ...

  2. [ 转载 ] Java 构造代码块

    https://blog.csdn.net/liyunxiangrxm/article/details/80244016

  3. BZOJ.5068.友好的生物(思路)

    题目链接 \(Description\) 求\[\max\{\sum_{i=1}^{k-1}(C_i*|a_{x,i}-a_{y,i}|)-C_k*|a_{x,k}-a_{y,k}|\}\] \(So ...

  4. poj 2253 最短路floyd **

    题意:有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的 ...

  5. Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑

    E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...

  6. Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增

    D. Animals and Puzzle 题目连接: http://codeforces.com/contest/713/problem/D Description Owl Sonya gave a ...

  7. 【汇总】PHP-FPM 配置优化(转)

    -----------------------开启php-fpm慢脚本日志 request_slowlog_timeout = 30sslowlog = /usr/local/php/var/log/ ...

  8. ASp.net中Froms验证方式

    微软的ASP.NET提供了3种用户验证方式,即Windows验证.窗体(Forms)验证和护照验证(Passport)验证. 由于验证方式各不相同,因而这3种验证方式在使用范围上也有很大的不同, Wi ...

  9. C++ STL set::find的用法

      参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - opera ...

  10. 魔兽私服TrinityCore 运行调试流程

    配置参见上一篇:TrinityCore 魔兽世界私服11159 完整配置 (1)启动Web服务器 打开TC2_Web_Mysql目录,运行“启动Web服务器.exe” 自动弹出帐号注册界面,并启动Ap ...