LeetCode_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
Analysis: Just recursively count the number of left sub tree times number of right sub tree.
- class Solution {
- public:
- int calculate(int n){
- int sum = ;
- for(int i = ; i< n ;i++ )
- sum += tree[i] * tree[n--i] ;
- // tree[i] : left subTree tree[n-1-i] " right subTree
- return sum ;
- }
- int numTrees(int n) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- tree.resize(n+,);
- for(int i = ; i <= n ; i++)
- tree[i] = calculate(i);
- return tree[n] ;
- }
- private :
- vector<int> tree;
- };
LeetCode_Unique Binary Search Trees的更多相关文章
- LeetCode_Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 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 ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Leetcode 86. Unique Binary Search Trees
本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...
- Print Common Nodes in Two Binary Search Trees
Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...
随机推荐
- java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
出现java cannot find the tag library descriptor for "http://java.sun.com/jsp/jstl/core" 主要的愿 ...
- glibc的了解,对内核的封装
glibc除了提供最底层的C运行库,还封装了kernel提供的API,程序通过glibc进行系统调用( syscall). 应用层面的C库,比如OpenSSL库: /usr/include/opens ...
- 专注于HTTP的高性能高易用性网络库:Fslib.network库
博客列表页:http://blog.fishlee.net/tag/fslib-network/ 原创FSLib.Network库(目前专注于HTTP的高性能高易用性网络库) FSLib.Networ ...
- 关于void*函数返回
一. sample #include<iostream> using namespace std; void* test(void* pass) { return pass; } int ...
- Linux内核设计基础(十)之内核开发与总结
(1)Linux层次结构: (2)Linux内核组成: 主要由进程调度(SCHED).内存管理(MM).虚拟文件系统(VFS).网络接口(NET)和进程间通信(IPC)等5个子系统组成. (3)与Un ...
- jre配置了1.6,但是eclipse仍然提示需要1.6以上的java
问题: eclipse创建web项目时,提示错误:Dynamic Web Module 3.0 requires Java 1.6 or newer. 原因: 这是因为当前的编译器java版本太低,请 ...
- log4net使用具体解释
说明:本程序演示怎样利用log4net记录程序日志信息.log4net是一个功能著名的开源日志记录组件.利用log4net能够方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包含M ...
- Win32多线程编程(1) — 基础概念篇
内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...
- Python进阶之路---1.2python版本差异
Python2.*与python3.*版本差异 作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习. Python版本差 ...
- mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接解决方法
mongodb下载地址(32位):下载地址 自己选择版本 建立如下与mongodb并行的两个文件夹data和log. 然后建立mongo.config. 在mongo.config配置文件中输入: # ...