1. Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
  2.  
  3. For example,
  4. Given n = 3, there are a total of 5 unique BST's.
  5. 1 3 3 2 1
  6. \ / / / \ \
  7. 3 2 1 1 3 2
  8. / / \ \
  9. 2 1 2 3

Analysis: Just recursively count the number of left sub tree times number of right sub tree.

  1. class Solution {
  2. public:
  3. int calculate(int n){
  4. int sum = ;
  5. for(int i = ; i< n ;i++ )
  6. sum += tree[i] * tree[n--i] ;
  7. // tree[i] : left subTree tree[n-1-i] " right subTree
  8. return sum ;
  9. }
  10. int numTrees(int n) {
  11. // Start typing your C/C++ solution below
  12. // DO NOT write int main() function
  13. tree.resize(n+,);
  14. for(int i = ; i <= n ; i++)
  15. tree[i] = calculate(i);
  16. return tree[n] ;
  17.  
  18. }
  19. private :
  20. vector<int> tree;
  21. };

LeetCode_Unique Binary Search Trees的更多相关文章

  1. 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. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  4. 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 ...

  5. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. LEETCODE —— Unique Binary Search Trees [动态规划]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. Leetcode 86. Unique Binary Search Trees

    本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...

  9. 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 ...

随机推荐

  1. 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" 主要的愿 ...

  2. glibc的了解,对内核的封装

    glibc除了提供最底层的C运行库,还封装了kernel提供的API,程序通过glibc进行系统调用( syscall). 应用层面的C库,比如OpenSSL库: /usr/include/opens ...

  3. 专注于HTTP的高性能高易用性网络库:Fslib.network库

    博客列表页:http://blog.fishlee.net/tag/fslib-network/ 原创FSLib.Network库(目前专注于HTTP的高性能高易用性网络库) FSLib.Networ ...

  4. 关于void*函数返回

    一. sample #include<iostream> using namespace std; void* test(void* pass) { return pass; } int ...

  5. Linux内核设计基础(十)之内核开发与总结

    (1)Linux层次结构: (2)Linux内核组成: 主要由进程调度(SCHED).内存管理(MM).虚拟文件系统(VFS).网络接口(NET)和进程间通信(IPC)等5个子系统组成. (3)与Un ...

  6. jre配置了1.6,但是eclipse仍然提示需要1.6以上的java

    问题: eclipse创建web项目时,提示错误:Dynamic Web Module 3.0 requires Java 1.6 or newer. 原因: 这是因为当前的编译器java版本太低,请 ...

  7. log4net使用具体解释

    说明:本程序演示怎样利用log4net记录程序日志信息.log4net是一个功能著名的开源日志记录组件.利用log4net能够方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包含M ...

  8. Win32多线程编程(1) — 基础概念篇

      内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...

  9. Python进阶之路---1.2python版本差异

    Python2.*与python3.*版本差异 作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习. Python版本差 ...

  10. mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接解决方法

    mongodb下载地址(32位):下载地址 自己选择版本 建立如下与mongodb并行的两个文件夹data和log. 然后建立mongo.config. 在mongo.config配置文件中输入: # ...