96. Unique Binary Search Trees && 95. Unique Binary Search Trees II
2016-01-27 18:11
465 查看
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.
分析:
BST,二分查找树。每一个节点,大于左子树,小于右子树,中序遍历是有序的。1~n能构造多少种不同的BST,可以用动态规划解决这个问题。用num[i]表示1~i能构造多少种不同的BST
首先,num[i]不仅代表1~i能构造多少种BST,也代表i个有序排列数字能有多少种BST。
其次num[i]的求法。num[i]=每一个数字作为根节点能构造的BST之和。
任一数字节点k =1~i 能构造的BST,左子树有k-1个节点,右子树有i-k个节点,分别能构造的子树个数为num[k-1]、num[i-k],则以k为根节点的BST为num[k-1]*num[i-k].递推关系确定。
最后,左子树有i个节点,右子树有j个节点,和右子树有i个节点,左子树有j个节点是一样的,k=1~i/2,和k = i/2+1~i 的计算存在对称性,不过根据奇偶有所不同。
Given 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.
这题要返回构造的BST,所以就不同用DP了,用BFS解决
For example,
Given n = 3, there are a total of 5 unique BST’s.
分析:
BST,二分查找树。每一个节点,大于左子树,小于右子树,中序遍历是有序的。1~n能构造多少种不同的BST,可以用动态规划解决这个问题。用num[i]表示1~i能构造多少种不同的BST
首先,num[i]不仅代表1~i能构造多少种BST,也代表i个有序排列数字能有多少种BST。
其次num[i]的求法。num[i]=每一个数字作为根节点能构造的BST之和。
任一数字节点k =1~i 能构造的BST,左子树有k-1个节点,右子树有i-k个节点,分别能构造的子树个数为num[k-1]、num[i-k],则以k为根节点的BST为num[k-1]*num[i-k].递推关系确定。
最后,左子树有i个节点,右子树有j个节点,和右子树有i个节点,左子树有j个节点是一样的,k=1~i/2,和k = i/2+1~i 的计算存在对称性,不过根据奇偶有所不同。
public int numTrees(int n) { if(n == 0) return 0; int[] num = new int[n+1]; num[0] = 1; num[1] = 1; for(int i = 2;i <= n;i++){ int total = 0; for(int j = 1;j <= i/2;j++){ int tmp = 0; tmp = num[j-1] * num[i-j]; total += tmp; } if(i % 2 == 0){ num[i] = 2*total; } else{ num[i] = 2*total+num[i/2]*num[i/2]; } } return num ; }
Given 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.
这题要返回构造的BST,所以就不同用DP了,用BFS解决
public List<TreeNode> generateTrees(int n) { List<TreeNode> r = new ArrayList<TreeNode>(); return generate(1,n); } private List<TreeNode> generate(int begin ,int end){ List<TreeNode> r = new ArrayList<TreeNode>(); if(begin > end){ r.add(null); return r; } if(begin == end){ TreeNode tmp = new TreeNode(begin); r.add(tmp); return r; } for(int i = begin;i <= end;i++){ List<TreeNode> left = generate(begin,i-1); List<TreeNode> right = generate(i+1,end); for(int j = 0;j < left.size();j++) for(int k = 0;k < right.size();k++){ TreeNode tmp = new TreeNode(i); tmp.left = left.get(j); tmp.right = right.get(k); r.add(tmp); } } return r; }
相关文章推荐
- Arduino Garden Controller - Automatic Watering and Data Logging
- Arduino based plant watering system using Soil Moisture Sensors
- 2、vuejs五脏
- hdu1121 Complete the Sequence
- HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树
- PIR Motion Sensor with Arduino
- 1,安装
- 关于UIView的autoresizingMask属性的研究【转】
- leetcode62-Unique Paths
- iOS-值对象NSValue介绍
- [转]SQL SERVER – Find Most Expensive Queries Using DMV
- 引导界面微场景交互设计与技术实现V2.0
- How to control your dust collection with an Arduino (it's easy)
- Arduino started (video)
- 入门动画篇之UIview动画(一)
- UITableView控制头部View
- solr Query
- POJ 3458 Colour Sequence(简单题)
- POJ 1986 Distance Queries(LCA)
- mysql Incorrect string value \xF0\x9F\x98\x84\xF0\x9F