题目

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

题解

 这道题我自己是没啥思路的,感觉就是一种排列组合的计算,但是又不太会。。然后网上查了一下,说要用卡特兰数。。
我下面的带引号的讲解引用Code Ganker(http://blog.csdn.net/linhuanmars/article/details/24761459)的了
“这道题要求可行的二叉查找树的数量,其实二叉查找树可以任意取根,只要满足中序遍历有序的要求就可以。从处理子问题的角度来看,选取一个结点为根,就把结点
切成左右子树,以这个结点为根的可行二叉树数量就是左右子树可行二叉树数量的乘积,所以总的数量是将以所有结点为根的可行结果累加起来。写成表达式如下:

熟悉卡特兰数的朋友可能已经发现了,这正是卡特兰数的一种定义方式,是一个典型的动态规划的定义方式(根据其实条件和递推式求解结果)。所以思路也很明确了,维护量res[i]表示含有i个结点的二叉查找树的数量。根据上述递推式依次求出1到n的的结果即可。

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

看到了一个解释的更清楚的分析,转自http://fisherlei.blogspot.com/2013/03/leetcode-unique-binary-search-trees.html:

这题想了好久才想清楚。其实如果把上例的顺序改一下,就可以看出规律了。

      1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

比如,以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]构成。

同时为了根据递推式来写程序,需要将递推式简化一下。

根据卡特兰数,C0Cn+1,因为leetcode输入的参数是n,所以为了避免混淆,这里递推式写成Ct+1,初始值为C0 = 1。

原始的递推式是: Ct+1 += Ci*Ct-i (0<= i <=t)

现在令变量num=t+1,那么t=num-1

所以原始递推式做变量替换得:Cnum += Ci*Cnum-1-i (0<= i <=num-1)

而num的取值范围是[1, n]因为C0已知。

代码如下:

 1     public int numTrees(int n) {
 2         if(n == 0||n == 1)
 3             return 1;
 4         
 5         int[] C = new int[n+1];
 6         C[0] = 1;
 7      //递推式是Ct+1 += Ci*Ct-i(0<= i <= t)
 8      //令num = t+1
 9      //则 t = num-1;
      //因此递推式化为:
      //Cnum += Ci*Cnum-1-i(0<=i<=num-1, 1<=num<=n)
      //C0 = 1
 
     for(int num=1;num<=n;num++){  
         for(int i=0;i<=num-1;i++){  
             C[num] += C[i]*C[(num-1)-i];  
         }  
     }
         return C[n];
     }

Unique Binary Search Trees leetcode java的更多相关文章

  1. leetcode 95 Unique Binary Search Trees II ----- java

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

  2. Unique Binary Search Trees [LeetCode]

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

  3. Unique Binary Search Trees——LeetCode

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

  4. Unique Binary Search Trees In JAVA

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

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

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

  6. Java for LeetCode 095 Unique Binary Search Trees II

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

  7. Unique Binary Search Trees II leetcode java

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

  8. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

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

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

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

随机推荐

  1. android 手机不能断点

    一个安卓平板 型号 联想 .. ,用andriod studio 打断点 调试, 应用 就卡死2秒,然后就挂掉了.  不知如何解决.

  2. RubyMine8 安装

    在win7下面安装RubyMine8 中文 汉化 1.需要下载 RubyMine8的程序包 2.先安装好 RubyMine8  很简单  点击下一步就可以  选择安装目录这应该就不需要说了 3.汉化 ...

  3. Android学习笔记PreferenceFragment的使用

    相信大家对Perference都比较熟悉了,也就是我们常说的偏好设置,首选项设置,可以保存一些数据,例如我们在上一次使用的时候的一些内容,希望在下一次启动后依然生效,而不需要再进行配置那么麻烦.一般这 ...

  4. 【Go命令教程】1. 标准命令详解

    Go 语言的 1.5 版本在标准命令方面有了重大变更.这倒不是说它们的用法有多大的变化,而是说它们的底层支持已经大变样了.让我们先来对比一下 $GOROOT/pkg/tool/< 平台相关目录 ...

  5. 调用WScript.Shell时产生Automation 服务器不能创建对象的错误

    我们经常需要通过生成ActiveXObject("WScript.Shell");来调某一exe文件, 如 //设置网页打印的页眉页脚为空 var HKEY_Root,HKEY_P ...

  6. Unity3D实践系列01,创建项目

    下载并安装Unity5软件客户端. 打开软件,注册Unity帐号,并用注册帐号登录. 点击"创建Project"按钮. 把项目命名为"My First Unity Pro ...

  7. C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能

    .Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...

  8. C#中使用throw和throw ex抛出异常的区别

    通常,我们使用try/catch/finally语句块来捕获异常,就像在这里说的.在抛出异常的时候,使用throw和throw ex有什么区别呢? 假设,按如下的方式调用几个方法: →在Main方法中 ...

  9. delphi 取得任意程序的命令行

    program GetCommandLineExDemo; uses Windows; constSystemHandleInformation = 16;ProcessBasicInformatio ...

  10. 用 setMessage 方法灵活复用 UIAlertView