本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43198929

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

思路:

(1)题意为给定n个节点,求能组成多少个二叉树。

(2)该题和给定n个数字,求其所有进栈出栈顺序总个数是相同的,详情参看数据结构。

(3)本题是运用递归进行实现。

递推关系为: f(n)=C(2n,n)/(n+1) (n=1,2,3,...)。

递归式为: f(n)=f(n-1)*(4*n-2)/(n+1)。

通过该公式进行递归即可得到答案。但是通过递归实现的算法效率显然要低一些。

递推过程:

当n=1时,只有1个根节点,则能组成1种,令n个节点可组成的二叉树数量表示为f(n),  则f(1)=1;

当n=2时,1个根节点固定,还有n-1个节点,可以作为左子树,也可以作为右子树, 即:f(2)=f(0)*f(1)+f(1)*f(0)=2,则能组成2种。

当n=3时,1个根节点固定,还有n-1=2个节点,可以在左子树或右子树, 即:f(3)=f(0)*f(2)+f(1)*f(1)+f(2)*f(0)=5,则能组成5种。

当n>=2时,有f(n)=f(0)*f(n-1)+f(1)*f(n-2)+...+f(n-1)*f(0)

(4)希望本文对你有所帮助。

算法代码实现如下:

	/**
	 * @author liqq
	 */
    public int numTrees(int n) {
        int i;
    	int result = 0;
    	if(n==0) return 1;
    	if(n==1) return 1;

    	for (i = n-1; i>=0 ; i--) {
			result = result + numTrees(i)*numTrees(n-i-1);
		}
    	return result;
    }

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. 2 Unique Binary Search Trees II_Leetcode

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

  4. 【leetcode】Unique Binary Search Trees (#96)

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

  5. LEETCODE —— Unique Binary Search Trees [动态规划]

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

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

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

  7. Leetcode 86. Unique Binary Search Trees

    本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...

  8. Print Common Nodes in Two Binary Search Trees

    Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...

  9. 【leetcode】Unique Binary Search Trees

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

随机推荐

  1. DB2 911错误的解释

    SQL0911N  因为死锁或超时,所以当前事务已回滚.原因码为 "<原因码>". 说明: 当前工作单元参与了未解决的对象争用,因此必须回滚. 原因码如下所示: 2   ...

  2. R语言:安装及使用

    http://blog.csdn.net/pipisorry/article/details/53640638 ubuntu下安装 sudo apt-get install -y r-base源码安装 ...

  3. Android样式(style)和主题(theme)

    样式和主题 样式是指为 View 或窗口指定外观和格式的属性集合.样式可以指定高度.填充.字体颜色.字号.背景色等许多属性. 样式是在与指定布局的 XML 不同的 XML 资源中进行定义. Andro ...

  4. Android艺术开发探索第三章——View的事件体系(上)

    Android艺术开发探索第三章----View的事件体系(上) 我们继续来看这本书,因为有点长,所以又分了上下,你在本片中将学习到 View基础知识 什么是View View的位置参数 Motion ...

  5. FORM开发两种方式实现动态LIST

    方法一:常规的,也是网上比较常见的 1.将目标ITEM的子类信息设置为List,不需要添加列表中元素,不需要初始值. 2.新建一个Procedure,代码如下: PROCEDURE basis_lis ...

  6. 协议系列之UDP协议

    上节说的TCP协议虽然提供了可靠的传输,但是也有一个缺点,发送速度慢.那么有没有一种协议能快速发送的呢?这节要讨论的就是UDP协议,它提供了更加快的发送速度,但也牺牲了可靠性,它是一种无连接的传输协议 ...

  7. Android Multimedia框架总结(十)Stagefright框架之音视频输出过程

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52560012 前言:上篇文中最后 ...

  8. java设计模式-----单例设计模式

    设计模式是个很高深的东西,我也是略懂皮毛,下面让我用最简洁易懂的语言描述下单例设计模式吧. 一些人总结出来用来解决特定问题的固定的解决方案. 解决一个类在内存中只存在一个对象,想要保证对象的唯一. 1 ...

  9. Android 字体设置-Typeface讲解

    控件的字体设置的两种方式 常用的字体类型名称还有: Typeface.DEFAULT //常规字体类型 Typeface.DEFAULT_BOLD //黑体字体类型 Typeface.MONOSPAC ...

  10. MyBatis Generator For Eclipse 插件安装

    由于在ORM框架MyBatis中,实现数据表于JavaBean映射时,配置的代码比较的复杂,所以为了加快开发的效率,MyBatis官方提供了一个Eclipse的插件, 我izuoyongjiushis ...