LeetCode 96: Unique Binary Search Tree
来源:互联网 发布:淘宝优品乐购 编辑:程序博客网 时间:2024/06/05 05:09
Difficulty: 3
Frequency: 1
Problem:
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
Solution:
class Solution {public: vector <int> unique; Solution() { unique.push_back(1); unique.push_back(1); } int numTrees(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if (n<unique.size()) return unique[n]; unique.resize(n+1, -1); int i_method = 0; for (int i = 1; i<=n; i++) { if (unique[i-1]==-1) unique[i-1] = numTrees(i-1); if (unique[n-i]==-1) unique[n-i] = numTrees(n-i); i_method += (unique[i-1]*unique[n-i]); } unique[n] = i_method; return unique[n]; }};
Notes:
DP.
- LeetCode 96: Unique Binary Search Tree
- leetCode 96:Unique Binary Search Tree
- LeetCode #96 Unique Binary Search Tree
- [LeetCode] Unique Binary Search Tree
- LeetCode : Unique Binary Search Tree
- 【LeetCode】Unique Binary Search Tree
- [Leetcode]Unique binary search tree
- Unique Binary Search Tree -- LeetCode
- LeetCode 96 Unique Binary Search Tree(Python详解及实现)
- Leetcode: Unique Binary Search Tree II
- Leetcode - Tree /Dynamic Programming- Unique Binary Search
- Leetcode练习- Unique Binary Search Tree
- leetcode java unique binary search tree
- 【Leetcode】Unique Binary Search Tree II (DP)
- 【Leetcode】Unique Binary Search Tree in JAVA
- leetcode-unique binary search tree II
- Unique Binary Search Tree II -LeetCode
- [leetcode][tree] Unique Binary Search Trees
- 深入学习Django源码基础8 - Django中系统级国际化本地化
- 基于键盘过滤驱动的键盘助手
- linux内核编译过程的最终总结版
- 每一个activity都要在AndroidManifest.xml注册和字体
- 50个高端大气上档次的管理后台界面模板
- LeetCode 96: Unique Binary Search Tree
- Android 网络链接判断
- UICC, CSIM, RUIM, UIM, USIM, SIM
- 网站原创文章为什么不被百度搜录的几种猜想
- Flyweight
- 软件架构的风险消除策略
- ios 限制只能输入字母和数字
- jqGrid使用笔记.
- JQuery的学习:DOM操作