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. 排序算法review<2>--Shell 排序

    shell排序方法也是一种插入排序算法,于1959年由D.L.Shell提出,其基本方法是:首先将带排序文件分为d1(d1<n)组,将所有彼此之间间隔为d和d的倍数的记录放在一组中,然后在组内进 ...

  2. python发送邮件的2种方式

    发送邮件的2种方式1.匿名发送 smtpObj = smtplib.SMTP(host, port) smtpObj.sendmail(from_addr, to_addrs, message.as_ ...

  3. java7(2)——使用mutilcatch注意事项

    从java7推出mutilcatch后,到现在都少有看到人使用,可能是这个功能真正用起来,比起多个catch并不快多少,而且现在的工IDE具太厉害了,什么都有快捷键!说是这么说,我们还是得了解一下如何 ...

  4. Android Volley全然解析(四),带你从源代码的角度理解Volley

    版权声明:本文出自郭霖的博客,转载必须注明出处. https://blog.csdn.net/sinyu890807/article/details/17656437 转载请注明出处:http://b ...

  5. Visual Studio 起始页面关闭新闻等

    [工具]->[选项]->[环境]->[启动] 将“下载内容的时间间隔”一项的勾选去掉,然后确定保存.这样,就大功告成啦

  6. Kafka笔记整理(二):Kafka Java API使用

    下面的测试代码使用的都是下面的topic: $ kafka-topics.sh --describe hadoop --zookeeper uplooking01:,uplooking02:,uplo ...

  7. Linux的日志管理

    Linux日志的管理 日志:记录了你几乎所有的操作记录,用于系统的审核,故障的排除.日志文件永久存放在日志目录中,系统日志保存在/var/log中 rsyslog 按照日志类型分类,把所有日志记录到/ ...

  8. rf调参小结

    转自http://www.cnblogs.com/pinard/p/6160412.html 1. scikit-learn随机森林类库概述 在scikit-learn中,RF的分类类是RandomF ...

  9. golang在线手册汇总

    1. golang官网 https://golang.org/ 2. golang中国 http://www.golangtc.com/ http://godoc.golangtc.com/pkg/ ...

  10. arya使用流程

    1.github中的项目clone到本地(路径在最后),然后将arya文件夹复制到你的django工程中作为一个独立的app,该app实现了RBAC(基于角色的权限访问控制Role-Based Acc ...