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. .svn批量删除

    c.bat @echo on @rem 删除SVN版本控制目录 @rem for /r . %%a in (.) do @if exist "%%a\.svn" @echo &qu ...

  2. Java SimpleDateFormat[转]

    [补充] [转] http://stackoverflow.com/questions/2603638/why-cant-this-simpledateformat-parse-this-date-s ...

  3. delphi使用outputdebugstring调试程序和写系统日志

    delphi使用outputdebugstring调试程序和写系统日志 procedure TForm1.btn1Click(Sender: TObject); begin OutputDebugSt ...

  4. 使用 Express 和 waterline 创建简单 Restful API

    这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000004996659  突然想起来我这个博客冷落了好多年了,也该更新一下, ...

  5. 简明易懂的call apply

    在iteye看到一篇对call解释得相当简明易懂,觉得得宣传一下 : http://uule.iteye.com/blog/1158829 一.方法的定义 call方法: 语法:call([thisO ...

  6. cocos2d-x-3.0 的改变,由于变得太多,一点点累积吧!

    1.cpp  改成  Point 2.setIsRelativeAnchorPoint() 改成  ignoreAnchorPointForPosition() 3.Layer::create   图 ...

  7. 【Linux】——ctags

    ctags的功能:扫描指定的源文件,找出其中所包含的语法元素,并将找到的相关内容记录下来. ctags 可以在官网上下载源代码,然后编译安装.最后在 ~/.vimrc 文件中写入以下配置: " ...

  8. iOS10 CoreData新特性

    原文地址:What's New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0 翻译者:肖品,原创文章转载请著名出处 ...

  9. mysql隔离级别

    MySQL/InnoDB定义的4种隔离级别: Read Uncommited 可以读取未提交记录. Read Committed (RC) 针对当前读,RC隔离级别保证对读取到的记录加锁 (记录锁), ...

  10. 谈"自驱力"

    最新说明: 1.标题是为了博眼球取的,请不大家不要纠结具体薪资数字,我瞎取的 2.请注意素质,不要满口喷粪,不要搞人身攻击,尊重别人,就是尊重你自己 3.请大家就事论事,不要胡乱臆想,请站在全局的角度 ...