Description:

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,计算出节点数为n的二叉查找树的种类。

这个题目最简单的思路应该是递归构造二叉查找树,让每个节点都成为根节点。这样时间复杂度较高。还有一种动态规划法。描述如下:

假如整个树有 n 个节点,根节点为 1 个节点,两个子树平分剩下的 n-1 个节点。

假设我们已经知道节点数量为 x 的二叉树有dp[x]种不同的形态。

则一颗二叉树左节点节点数量为 k 时,其形态数为dp[k] * dp[n - 1 - k]

而对于一颗 n 个节点的二叉树,其两个子树分配节点的方案有 n-1 种:

(0, n-1), (1, n-2), ..., (n-1, 0)

因此我们可以得到对于 n 个节点的二叉树,其形态有:

Sigma(dp[i] * dp[n-1-i]) | i = 0 .. n-1

并且可以发现,dp数组有递推关系,我们可以使用递推或是记忆化搜索来实现。

边界条件为dp[0] = 1

以上分析来源这里

Java代码:

public class Solution {

    int[] rc;

    public int numTrees(int n) {
rc = new int[n+1];
Arrays.fill(rc, 0);
return dp(n);
} public int dp(int nodes) {
if(nodes <= 1)
return 1;
if(rc[nodes] != 0)
return rc[nodes]; int numTrees = 0;
for(int i=0; i<nodes; i++) {
numTrees += dp(i) * dp(nodes-i-1);
}
rc[nodes] = numTrees; return numTrees;
}
}

LeetCode-96. 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. 52. leetcode 96. Unique Binary Search Trees

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

  3. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  4. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

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

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

  6. leetcode 96 Unique Binary Search Trees ----- java

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

  7. Java [Leetcode 96]Unique Binary Search Trees

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

  8. [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数

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

  9. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

  10. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

随机推荐

  1. 《软件性能测试与LoadRunner实战教程》新书上市

    作者前三本书<软件性能测试与LoadRunner实战>.<精通软件性能测试与LoadRunner实战>和<精通软件性能测试与LoadRunner最佳实战>面市后,受 ...

  2. 【VerySky原创】 ME9F

    [VerySky原创] V_EKKONA - Generierte Tabelle zu einem View V_EKKONA-PFLD4 字段 SE71查看 至于图片:在表STXBITMAPS中 ...

  3. csdn 泄露用户密码害人不浅啊。

    先是京东被盗,接着博客园也登陆不了了.

  4. [C] zlstdint(让VC、TC等编译器自动兼容C99的整数类型)V1.0。支持Turbo C++ 3等DOS下的编译器

    作者:zyl910 以前我曾为了让VC++等编译器支持C99的整数类型,便编写了c99int库来智能处理(http://www.cnblogs.com/zyl910/p/c99int_v102.htm ...

  5. halcon学习之产品检测

    Rinspect_gasket_local_deformable.hdev   检测垫圈局部变形   *这个例子演示了如何利用局部变形匹配(local deformable matching)来寻找出 ...

  6. 一次完整的HTTP事务分析

    在浏览器中输入一个地址,按下回车之后,到用户看到页面之前,发生了什么? https://www.processon.com/view/link/56c6679ce4b0f0c4285e69c0

  7. 常用jquery插件资料

    fullPage.js 全屏滚动https://github.com/alvarotrigo/fullPage.js Lava Lamp 导航条熔岩灯http://lavalamp.magicmedi ...

  8. Web app 的性能瓶颈与性能调优方法

    1. web app 性能测试工具使用 2. mysql 性能分析与调优方法

  9. C#判断一个string是否为数字

    案一:Try...Catch(执行效率不高) private bool IsNumberic(string oText) { try { int var1=Convert.ToInt32 (oText ...

  10. No Assistant Results

    由于修改一些文件名字等会导致这个不工作. "Organizer" / "Projects" / 选择你的项目.  "Delete" .