【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. 2017国家集训队作业[agc016e]Poor Turkey

    2017国家集训队作业[agc016e]Poor Turkey 题意: 一开始有\(N\)只鸡是活着的,有\(M\)个时刻,每个时刻有两个数\(X_i,Y_i\),表示在第\(i\)个时刻在\(X_i ...

  2. UVA - 10674-Tangents

     题意:给出两个圆,求它们的公切线,并依照一定格式输出 做法:模拟 代码: #include<iostream> #include<map> #include<str ...

  3. scrollBarStyle- listview滑动条调整

    今天在解决问题的时候,需要设置listview滚动条,使listview的内容不能被滚动条覆盖 后来发现网上提到一个 ScrollView 属性.ScrollView中ScrollBar的style ...

  4. spring的BeanWrapper类的原理和使用方法

    转自:http://blog.sina.com.cn/s/blog_79ae79b30100t4hh.html 如果动态设置一个对象属性,可以借助Java的Reflection机制完成: Class ...

  5. Java Servlet学习笔记(四)Servlet客户端Http请求

    Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的.您可以查看 HTTP ...

  6. 安装Mysql最新版本mysql-5.7.10-winx64出现的几个问题解决

    电脑是64位的安装不了Windows (x86, 32-bit),Mysql installer MSI ,然后下载了Windows (x86, 32-bit), ZIP Archive 这种是免安装 ...

  7. Caused by: java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.getServletContext()L

    在做项目的时候,出现Caused by: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletCo ...

  8. 3.lombok系列3:lombok的实验类特性

    转自:https://blog.csdn.net/54powerman/article/details/72516755 lombok除了已经推荐使用的基本功能,还维护了一个创新型的注解,有些功能有违 ...

  9. Android Intent的setClass和setClassName的区别

    setClass:跳转到与该工程下的(同一个Application中的)activity或者service setClassName:跳转到不同Applicaiton的activity或者servic ...

  10. Linux 从core信息中找到TLS信息

    背景 我们在查core问题时,有时候须要查看某个TLS变量的值.可是GDB没有提供直接的命令,或者我不知道.这篇文字的目的.就是想办法从core文件里找出某个线程存放TLS变量的内容. 依据 Linu ...