题目链接

https://leetcode.com/problems/unique-binary-search-trees/

题意

计算给定节点数的BST有多少种

思路

递归

相关知识

二叉搜索树(Binary Search Tree ,BST)

A BST is defined as follows:

1 The left subtree of a node contains only nodes with keys less than the node's key.

2 The right subtree of a node contains only nodes with keys greater than the node's key.

3 Both the left and right subtrees must also be binary search trees.

特点:

1.每个节点的值大于其任意左侧子节点的值,小于其任意右节点的值。

2.平衡时性能逼近二分查找,但相比连续内存空间的二分查找,插入删除操作开销更小,但多次插入删除可能造成树的不平衡,最坏接近线性查找。

代码

class Solution {
public:
int numTrees(int n) {
if(n==0||n==1){return 1;}
else{
int BSTCnt=0;
for(int i=1;i<=n;i++){
BSTCnt+=numTrees(i-1)*numTrees(n-i);
}
return BSTCnt;
}
}
};

reference

https://www.cnblogs.com/Leo_wl/p/5229585.html

[LeetCode_96] Unique Binary Search Trees的更多相关文章

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

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

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

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

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

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

  4. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  5. 【leetcode】Unique Binary Search Trees II

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

  6. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  7. LeetCode: Unique Binary Search Trees II 解题报告

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

  8. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. LeetCode:Unique Binary Search Trees I II

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

随机推荐

  1. WPF 透明窗体

    窗体属性中设置:Background="Transparent" AllowsTransparency="True" WindowStyle="Non ...

  2. https 学习总结

    最近看了点https 做下总结  面的博客如果没有错误的话,理解起来绝对是醍醐灌顶!让人信服,如果我的理解有问题,请及时指正! 参考博客:    http://www.ruanyifeng.com/b ...

  3. ElasticSearch centos7 安装

    参考: https://blog.csdn.net/u014180504/article/details/78733827 https://blog.csdn.net/youzhouliu/artic ...

  4. js判断对象

    一般学java的小伙伴,刚开始写js时如果遇到要判断一个字符串是否不为空,往往会这样写 if(str != undefined && str != null && st ...

  5. 解决 'Could not convert variant of type (NULL) into type (String)

    写存储过程中有不允许为空的字段,在客户端转化取数时显示 Could not convert variant of type (NULL) into type (String) 可以在存储过程中使用is ...

  6. LeetCode OJ 93. Restore IP Addresses

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  7. PHP 操作Mongodb 实例

    缩略版本<?php //1.连接MongoDB $mongo = new Mongo(); $mongo = new Mongo("mongodb://username:passwor ...

  8. 免費查看SQL PLAN的工具 - SQL Sentry Plan Explorer

    今天 Terry大 介紹給小弟這個 SQL Sentry Plan Explorer 工具,可以用來看SQL Plan. 什麼? 用SSMS看不就很清楚了嗎? 這個Tool有把SQL Plan幫我們整 ...

  9. 记Dagger2使用过程中的一个BUG--compileGoogleDebugJavaWithJavac

    项目编译可以通过,不过没有生成Dagger2的类,导致无法运行项目.. 错误提示 Error:(14, 41) 错误: 找不到符号 符号: 类 DaggerAppComponent 位置: 程序包 c ...

  10. Hibernate 再接触 Hello world 模拟Hibernate

    没有Hibernate以前 Cilent 客户端 new出一个对象 然后执行JDBC 然后这样的访问数据库并不是面向对象语言 使用Hibernate以后 Cilent new 出一个对象后 访问配置文 ...