LeetCode 96. Unique Binary Search Trees
2016-07-19 16:32
507 查看
Problem: https://leetcode.com/problems/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.
Thought:
For bst of 1...n, it can be structured bu root i and left tree 1...i and right tree i+1....n, so dp is available
Code C++:
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
Thought:
For bst of 1...n, it can be structured bu root i and left tree 1...i and right tree i+1....n, so dp is available
Code C++:
class Solution { public: int numTrees(int n) { int *num = new int[n + 1]; for (int i = 0; i <= n; i++) { num[i] = 0; } num[0] = 1; num[1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i; j++) { num[i] += num[j - 1] * num[i - j]; } } return num ; } };
相关文章推荐
- UE4事件相关总结
- UE4异步载入资源
- Phoronix-test-suite benchmark toolkit
- iOS学习 数据库 FMDB框架使用 UISearchBar搜索框 线程安全,事务
- Android奇怪错误---java.lang.NumberFormatException: Color value '@drawable/C-t' must start with ‘#’
- Servlet学习(八)request转发
- UUID实现
- 成长小计-控制器中有UIScrollView控件时,无法响应触摸事件
- 【翻译】Fluent NHibernate介绍和入门指南
- easyui datagrid 表格组件列属性formatter和styler使用方法
- ubuntu系统安装arduino流程
- 在Kubernetes上搭建EFK(Fluentd+Elasticsearch+Kibana)
- TestNG测试报告美化buid.xml配置
- UE4 减少APK包的大小
- UUID详解
- Leetcode题集——permutation-sequence
- UE4 性能优化方法(工具篇)
- Java StringBuffer和StringBuilder类
- 解决ntp的错误 no server suitable for synchronization found
- 解决UICollectionView的Cell复用引起的布局混乱问题