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

解题思路:

设有n个结点的二叉查找树有b[n]个,则设想对一个排好序的list,我们从第一个元素开始枚举根节点。对于每一个根节点,这棵二叉查找树的可能是b[left] * b[right], left < n, right < n。

故我们只需要取b[0] = 0, b[1] = 1, 然后迭代计算b[i]即可。

class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(n == )
return ; int *num;
num = new int[n + ];
num[] = ;
num[] = ;
for(int i = ;i <= n;i++)
{
num[i] = ;
for(int j = ;j < i;j++)
num[i] += num[j] * num[i - - j];
} return num[n];
}
};

[LeetCode] Unique Binary Search Tree的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. Unique Binary Search Tree II

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

  3. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  4. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. sort 排序 自定义排序算法的使用

    // struct sort_by_pt// {// bool operator()(const std::pair<CString, AcGePoint3d> a, const std: ...

  2. 视频剪辑生成gif格式(php外挂python程序)完美!

    接到朋友的需求,朋友是做php的,让我帮忙处理php生成gif的需求.他的项目类似抖音短视频那种,就是展示出来的界面是gif动图,然后点进去是完整的视频. 我想了想,我倒是没做过php生成gif的需求 ...

  3. Day 14B 网络应用开发

    网络应用开发 发送电子邮件 在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知.网站向用户发送一个激活账号的链接.银行向客户推广它们的理财产品等几乎都 ...

  4. java命令行版的ATM

    import java.util.*;public class Jatm{ static String user = "123"; static String password = ...

  5. linux shell脚本学习笔记一

    一.文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tm ...

  6. CSS 嵌入,及其选择器

    CSS 1. CSS样式表的几种使用方式 1.元素内嵌 <p style="font-size"></p> 2.内部文档内嵌 <style type= ...

  7. session对象的使用

    session对象的使用 制作人:全心全意 session在网络中被称为会话.由于HTTP协议是一种无状态协议,也就是当一个客户向服务器发出请求,服务器接收请求,并返回响应后,该连接就结束了,而服务器 ...

  8. Ioc思想

    DIP: Dependency Inversion Principle 依赖倒转原则 高层次组件不应该依赖于低层次组件,二者均应该依赖于接口.抽象不应该依赖于细节,细节应该依赖于抽象. IOC:Inv ...

  9. nyoj 911 Registration system(map)

    Registration system 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 A new e-mail service "Berlandesk&q ...

  10. HDU 1573 解同余模线性方程组

    题目意思很直接就是一道裸的解线性同余模方程组的题目 #include <cstdio> #include <cstring> using namespace std; #def ...