一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:给定一个数n,找出1~n组成的二叉搜索树的个数!

可以参考【一天一道LeetCode】#95. Unique Binary Search Trees II

如果单单求个数的话,可以简化。

n=1时,num=1;n=2时,num=2;n=3时,num=5!

可以找出规律,num[i] = num[left]*num[right]!i从1取到n,就为num[n]的值。left和right为左右的节点个数。(节点个数小于等于1的时候记二叉搜索树个数为1)

以题目中的例子为例,取1为根节点,2,3组成的二叉搜索树个数为2(right),左边为1,所以1为根节点的二叉搜索树个数为2,依次可以算出,2为根节点时个数为1,3为根节点的个数为2,加起来为5!

class Solution {
public:
    int numTrees(int n) {
        vector<int> num(n+1,-1);//存放i个节点存放的二叉搜索树个数
        int ret = calNumTrees(1,n,num);
        return ret;
    }
    int calNumTrees(int start , int end , vector<int>& num)
    {
        if(num[end - start +1] != -1) return num[end - start +1];
        if(start >= end) return 1;//当少于等于1个节点时,二叉搜索树个数记为1
        int temp = 0;
        for(int i = start ; i <= end ; i++)//依次以1到n为根节点
        {
            int left = calNumTrees(start,i-1,num);//左边二叉搜索树的个数
            int right = calNumTrees(i+1,end,num);//右边二叉搜索树的个数
            temp +=(left*right);
        }
        if(num[end - start +1] == -1) num[end-start+1] = temp;//num[i]存放1~i组成的个数
        return temp;
    }
};

【一天一道LeetCode】#96. Unique Binary Search Trees的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

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

  2. 52. leetcode 96. Unique Binary Search Trees

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

  3. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  4. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

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

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

  6. leetcode 96 Unique Binary Search Trees ----- java

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

  7. Java [Leetcode 96]Unique Binary Search Trees

    题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...

  8. [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数

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

  9. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

  10. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

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

随机推荐

  1. jquery checkbox是否选中

    $("#chkDisplayZxOnly").is(":checked")  选中返回true,否则返回false

  2. localStorage存储数组以及取数组方法

    var weekArray = ['周一'.'周二'.'周三'.'周四'.'周五']; //存: localStorage.setItem('weekDay',JSON.stringify(weekA ...

  3. IntelliJ IDEA光标变粗 backspace无法删除内容解决方法

    进入了vim插件 1.ctrl+alt+s快捷键打开Settings 2.选择左侧列表中的Plugins 3.在右侧面板的搜索框中搜索IdeaVim 4.将复选框中的钩子去掉 backspace成了其 ...

  4. Tomcat出现validateJarFile-jar not loaded问题

    tomcat启动时问题: validateJarFile(...\WEB-INF\lib\servlet-api.jar)-jar not loaded. See Servlet Spec 2.3, ...

  5. web性能优化之--合理使用http缓存和localStorage做资源缓存

    一.前言 开始先扯点别的: 估计很多前端er的同学应该遇到过:在旧项目中添加新的功能模块.或者修改一些静态文件时候,当代码部署到线上之后,需求方验收OK,此时你送了一口气,当你准备开始得意于自己的ma ...

  6. 修改原生单选框样式(vue单选组件)

    一.效果如图 二.实现 修改单选样式 //html <div class="radio-wrap"> <input type="radio" ...

  7. python学习之路网络编程篇(第四篇)

    python学习之路网络编程篇(第四篇) 内容待补充

  8. 1. 两数之和 LeetCode

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [, , , ], target = 因为 n ...

  9. User-Agent-Switcher和fiddler

    浏览器模拟器(可以模拟各种浏览器效果,浏览器中看手机显示的效果) http://chromecj.com/web-development/2014-09/70.html User-Agent-Swit ...

  10. Android Multimedia框架总结(二十五)MediaProjection实现手机截屏(无须root)

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/53966818 前言:一年半多以前 ...