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

算法分析:类似上阶梯,简单的动态规划问题。当根节点为i时,比i小的节点有i-1个,比i大的节点有n-i个,所以,i为根节点能够生成二叉排序树的个数是

nums[n] += nums[i-1]*nums[n-i],i从1到n。

public class UniqueBinarySearchTrees
{
public int numTrees(int n)
{
if(n <= 0)
{
return 0;
}
int[] res = new int[n+1];
res[0] = 1;
res[1] = 1;
for(int i = 2; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)//j为根节点
{
res[i] += res[j-1]*res[i-j];
}
}
return res[n];
}
}

Unique Binary Search Trees2:求生成二叉排序树的根节点的集合

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

For example,
Given n = 3, your program should return all 5 unique BST's shown below.

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

算法分析:这个不是求个数,而是求生成树根节点。使用递归。

public class UniqueBinarySearchTreesII
{
public List<TreeNode> generateTrees(int n)
{
if(n <= 0)
{
return new ArrayList<TreeNode>();
} return helper(1, n);
} public List<TreeNode> helper(int m, int n)
{
List<TreeNode> res = new ArrayList<>();
if(m > n)
{
res.add(null);
return res;
} for(int i = m; i <= n; i ++)
{
//i为根节点
List<TreeNode> ls = helper(m, i-1);//i节点的左子树
List<TreeNode> rs = helper(i+1, n);//i节点的右子树
for(TreeNode l : ls)
{
for(TreeNode r : rs)
{
TreeNode curr = new TreeNode(i);
curr.left = l;
curr.right = r;
res.add(curr);
}
}
}
return res;
}
}

Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树的更多相关文章

  1. 41. 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 ...

  2. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

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

  4. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  5. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

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

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

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

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

  8. 2 Unique Binary Search Trees II_Leetcode

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

  9. 【leetcode】Unique Binary Search Trees (#96)

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

随机推荐

  1. opencv亚像素级角点检测

    一般角点检测: harris cv::cornerHarris() shi-tomasi cv::goodFeaturesToTrack() 亚像素级角点检测是在一般角点检测基础之上将检测出的角点精确 ...

  2. POJ1330Nearest Common Ancestors最近公共祖先LCA问题

    用的离线算法Tarjan 该算法的详细解释请戳 http://www.cnblogs.com/Findxiaoxun/p/3428516.html 做这个题的时候,直接把1470的代码copy过来,改 ...

  3. Number Sequence---hdu1711(kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 题意就是求b数组在a数组出现的位置:就是kmp模板: #include<stdio.h&g ...

  4. mysql设置远程访问之后 远程访问非常缓慢 解决办法!

    在mysql配置文件的 选项下添加设置. [mysqld] skip-name-resolve skip-name-resolve

  5. Flask之flask-migrate

    简介 flask-migrate是flask的一个扩展模块,主要是扩展数据库表结构的. 官方文档:http://flask-migrate.readthedocs.io/en/latest/ 使用fl ...

  6. spring 拾遗

    1.@PostConstruct VS  init-method 1.1 both BeanPostProcessor 1.2 @PostConstruct is a JSR-250 annotati ...

  7. Mybatis框架学习总结-表的关联查询

    一对一关联 创建表和数据:创建一张教师表和班级表,这里假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关系. CREATE TABLE teacher( t_id INT PRIM ...

  8. PAT 1093 Count PAT's[比较]

    1093 Count PAT's (25 分) The string APPAPT contains two PAT's as substrings. The first one is formed ...

  9. Code signing is required for product type 'Application' in SDK 'iOS 11.2'

    在打包的时候出现这样一个错误,Code signing is required for product type 'Application' in SDK 'iOS 11.2'  ,就是说代码签名证书 ...

  10. dubbo-admin 部署

    上一章主要是谈到zookeeper的安装和部署 因为zookeeper只是一个黑框,我们无法看到是否存在了什么提供者或消费者,这时就要借助Dubbo-Admin管理平台来实时的查看,也可以通过这个平台 ...