Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树
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;}
}
转载于:https://www.cnblogs.com/masterlibin/p/5827259.html
Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树相关推荐
- [LeetCode]: 96: Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- 【Unique Binary Search Trees II】cpp
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode: Unique Binary Search Trees [095]
[题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- 【LeetCode从零单排】No96 Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex ...
- LeetCode:Unique Binary Search Trees
问题描写叙述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? F ...
- leetcode 95. Unique Binary Search Trees II | 96. Unique Binary Search Trees
95. Unique Binary Search Trees II https://leetcode.com/problems/unique-binary-search-trees-ii/ 题解 题是 ...
- 96. Unique Binary Search Trees(I 和 II)
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- [swift] LeetCode 96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...
- LeetCode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- Unique Binary Search Trees ll -深度优先遍历DFS
题目:Unique Binary Search Trees ll English: Given an integer n, generate all structurally unique BST's ...
最新文章
- 学会理解并编辑/etc/fstab
- Android进程间通信(IPC)机制Binder简要介绍和学习计划
- CG-CTF-Web-bypass again
- Cognos报表展示图片小技巧
- [异常解决] ubuntu上安采用sudo启动的firefox,ibus输入法失效问题解决
- 协议转换器的特点有哪些?
- 外媒称苹果挖走特斯拉高管 可能将重启电动汽车开发
- 订阅发布系统得解耦与冗余
- 在线民宿满意度测评项目[开源]
- windows下 MongoDB 导入json文件
- Web---HTTP请求、重定向、转发和数据压缩
- 830计算机软件综合大纲,2019年中国地质大学830计算机软件综合考试大纲.pdf
- 转载《2020年校招面经心得》
- Android开发 环境搭建(ubuntu)
- Oracle语句函数
- android平台多路摄像头实现方式的简析
- 微型计算机原理考试试卷,微机原理试题集试题库(带答案解析)
- 华为k662c的虚拟服务器,华为k662c光猫怎么样? 华为K662c拆机技巧
- 带2nd的计算机软件,2nd在计算器中是什么意思
- fest556_FEST JavaFX编译器Maven插件