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

时间:2022-09-03 23:05:41

[Leetcode] Unique binary search trees 唯一二叉搜索树

Unique Binary Search Trees leetcode java

描述

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

解析

递归

思路:空树和只有根节点时,也为BST。对于一点i,当其为根节点时,左子树的节点的个数为i-1,(为1,...i-1),右子树的个数为n-i(为,i+1,...n)。对一个根来说,唯一二叉树的个数为左子树结点的个数乘以右子树的个数。而根节点可以从1到n 中选择。

可行的二叉查找树的数量,其实二叉查找树可以任意取根,只要满足中序遍历有序的要求就可以。从处理子问题的角度来看,选取一个结点为根,就把结点切成左右子树,以这个结点为根的可行二叉树数量就是左右子树可行二叉树数量的乘积,所以总的数量是将以所有结点为根的可行结果累加起来。

动态规划

这是Catalan Number卡特兰数的一个例子。卡特兰数的的递推公式:

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

根据图示

以1为根的树有几个,完全取决于有二个元素的子树有几种。同理,2为根的子树取决于一个元素的子树有几个。以3为根的情况,则与1相同。

定义Count[i] 为以[0,i]能产生的Unique Binary Tree的数目,

如果数组为空,毫无疑问,只有一种BST,即空树,
    Count[0] =1

如果数组仅有一个元素{1},只有一种BST,单个节点
    Count[1] = 1

如果数组有两个元素{1,2}, 那么有如下两种可能
    1                       2
     \                    /
       2                1
    Count[2] = Count[0] * Count[1]   (1为根的情况)
                  + Count[1] * Count[0]  (2为根的情况。

再看一遍三个元素的数组,可以发现BST的取值方式如下:
    Count[3] = Count[0]*Count[2]  (1为根的情况)
                  + Count[1]*Count[1]  (2为根的情况)
                  + Count[2]*Count[0]  (3为根的情况)

所以,由此观察,可以得出Count的递推公式为
    Count[i] = ∑ Count[0...k] * [ k+1....i]     0<=k<i-1
    问题至此划归为一维动态规划。

[Note]
    这是很有意思的一个题。刚拿到这题的时候,完全不知道从那下手,因为对于BST是否Unique,很难判断。最后引入了一个条件以后,立即就清晰了,即
    当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:
   以i为根节点的树,其左子树由[1, i-1]构成, 其右子树由[i+1, n]构成。

维护量res[i]表示含有i个结点的二叉查找树的数量。

根据上述递推式依次求出1到n的的结果即可。

用一个数组保存 1 至 n-1 对应的不同二叉树的个数 X1、X2、X3、... Xn-1

则 n 对应的不同二叉树个数Xn = Xn-1 + X1*Xn-2 + X2*Xn-3 + X3*Xn-4 + ... + Xn-2*X1 + Xn-1

通过这个递推式,我们可以从 N = 1 开始递推,最后得到 N = n 时不同二叉查找树的个数。

时间上每次求解i个结点的二叉查找树数量的需要一个i步的循环,总体要求n次,所以总时间复杂度是O(1+2+...+n)=O(n^2)。空间上需要一个数组来维护,并且需要前i个的所有信息,所以是O(n)。

代码

递归

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

动态规划

class Solution {
public int numTrees(int n) {
if (n <= 0)
return 0;
int[] res = new int[n + 1];
res[0] = 1;
res[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < i; j++) {
res[i] += res[j] * res[i - j - 1];
}
}
return res[n];
}
}

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

  1. &lbrack;leetcode&rsqb;96&period; Unique Binary Search Trees给定节点形成不同BST的个数

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

  2. &lbrack;LeetCode&rsqb; 96&period; Unique Binary Search Trees 唯一二叉搜索树

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

  3. &lbrack;LeetCode&rsqb; 95&period; Unique Binary Search Trees II&lpar;给定一个数字n,返回所有二叉搜索树&rpar; &star;&star;&star;

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  4. 52&period; leetcode 96&period; Unique Binary Search Trees

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

  5. leetcode 96&period; Unique Binary Search Trees 、95&period; Unique Binary Search Trees II 、241&period; 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]* ...

  6. &lbrack;LeetCode&rsqb; 96&period; Unique Binary Search Trees 独一无二的二叉搜索树

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

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

  8. Java &lbrack;Leetcode 96&rsqb;Unique Binary Search Trees

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

  9. &lbrack;leetcode&rsqb; 96 Unique Binary Search Trees &lpar;Medium&rpar;

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

随机推荐

  1. leetcode&colon;32 最长有效括号

     题目: 给一个包含了'(' 和 ')'的字符串,求出其中最长有效括号的长度. 做题情况:自己做出来,但做了较长的时间. 思路:可以算得穷举法的时间复杂度为O(n^3).虽然这题求的是最长的长度,但是 ...

  2. 深入理解计算机系统家庭作业汇总 20135301&amp&semi;&amp&semi;20135328

    深入理解计算机系统家庭作业 深入理解计算机系统第二章家庭作业 题目2.64 题目要求 判断二进制数偶数位是否有任意一位位为1,有的话返回1,否则返回0 解题过程 int any_even_one(un ...

  3. HDFS的基本shell操作,hadoop fs操作命令

    (1)分布式文件系统 随着数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管 ...

  4. JS实现跳转到页面任何地方

    要实现两个内容: 1.从A页面跳转到B页面任何地方 方法:用id对要跳转的地方进行标记. 首先,在A页面可以设一个链接 <a href = "b.html#pos" targ ...

  5. &OpenCurlyDoubleQuote;layout&lowbar;”下划线开头的属性

    观看幕课网的视频的收获 “layout_”下划线开头的属性都是交给父容器去处理的属性,如: android:layout_width="match_parent" android: ...

  6. Java学习----方法的重载

    一个类中有多个同名的参数不一样的方法. 作用:可以根据不同的条件调用不同的方法. 注意:java不会因为方法的返回类型或者权限的不同而判断为不同的两个方法. public class Student ...

  7. css hr 设置

    http://www.sovavsiti.cz/css/hr.html http://adamculpepper.net/blog/css/hr-tag-css-styling-cross-brows ...

  8. shell编程之文本与日志过滤

    1:grep命令: grep -v  "char"  file_name 匹配不包括"char"的文本 grep -n -w "char" ...

  9. UWP自定义RadioButton实现Tab底部导航

    先看效果: 参照Android的实现方式用RadioButton来实现,但是Uwp的RadioButton并没有安卓的Selector选择器 下面是一个比较简单的实现,如果有同学有更好的实现,欢迎留言 ...

  10. Python函数的一点用法

    #python的基本语法网上已经有很多详细的解释了,写在这里方便自己记忆一些 BIF是python内置的函数,任何一门语言都能用来创造函数,python也不例外 1.创建一个函数 def func() ...