【096-Unique Binary Search Trees(唯一二叉搜索树)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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

题目大意

  给定一个n个结点的二叉搜索树,求一共同拥有多少个不同类型的二叉搜索树。

解题思路

  递推公式

  f(k)*f(n-1-k):f(k)表示根结点左子树有k个结点。其有的形状是f(k),f(n-1-k)表示右子树有n-1-k个结点

  f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)*f(n-3) + f(3)*f(n-4) + … +f(n-2)*f(1)

代码实现

算法实现类

public class Solution {

    public int numTrees(int n) {

        if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} int[] result = new int[n + 1];
result[0] = 0;
result[1] = 1; // 求f(2)...f(n)
for (int i = 2; i <= n; i++) {
// 求f(i)
result[i] = 2 * result[i - 1];
for (int j = 1; j <= i - 1 ; j++) {
result[i] += result[j]*result[i - 1 -j];
} }
return result[n];
}
}

评測结果

  点击图片。鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47333321

【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  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 独一无二的二叉搜索树

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

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

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

  5. 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 ...

  6. 096 Unique Binary Search Trees 不同的二叉查找树

    给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树:   1         3     3      2      1    ...

  7. LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  9. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  10. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. BZOJ1835: [ZJOI2010]base 基站选址(线段树优化Dp)

    Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄 ...

  2. windows10系统window键失灵,没有反应

    今天键盘的的Window键(win键)按了没反应,某度一圈全是它的垃圾营销号文章,没卵用..最后在微软官方社区支持找到解决方案.也建议大家遇到系统问题到微软社区去寻求帮助,毕竟人家是专业. 解决办法 ...

  3. JNDI学习总结(4)——Tomcat下使用Druid配置JNDI数据源

    com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...

  4. 将字符串使用md5加密

    >>> import md5 >>> md5.md5('123').hexdigest() '202cb962ac59075b964b07152d234b70' & ...

  5. makeMtk- user 版本编译

    有时候我们需要在我们的手机上编译user 版本,先说一下user 跟eng版本的区别 user:这个版本是没有root权限的,当你adb shell进入后,linux下显示的是$,不可以push ap ...

  6. node:json与csv互转

    [单个文件的转化]   1.安装json2csv模块将json转成csv   jsonToCSV.js var fs = require('fs'); const Json2csvParser = r ...

  7. CISP/CISA 每日一题 22

    CISSP 每日一题(答)What should be done to verify patcheshave been applied? Auditpatches, or use a vulnerab ...

  8. eclipse4.3怎么集成jadclipse追踪源代码,现在windows-preferences-java

      A.将net.sf.jadclipse_3.2.4.jar复制到D:\leaf\eclipse\plugins目录下.  B.在d:\leaf下建立ecliplsePlungin\jadclips ...

  9. 巧用数据流让 Word 文档在线阅读

            常常写博客或空间日记的朋友,对网络编辑器(如图1,是CSDN的博客编辑器)并不陌生.也比較easy做出非常绚烂的排版.但这次在做一个BS的项目,客户一直在用Office的软件中的Wor ...

  10. android-铃声的设置与播放

    在android系统中,不同铃声存放的铃声路径: /system/media/audio/ringtones 来电铃声 /system/media/audio/notifications 短信通知铃声 ...