LeetCode:Unique Binary Search Trees

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

分析:依次把每个节点作为根节点,左边节点作为左子树,右边节点作为右子树,那么总的数目等于左子树数目*右子树数目,实际只要求出前半部分节点作为根节点的树的数目,然后乘以2(奇数个节点还要加上中间节点作为根的二叉树数目)

递归代码:为了避免重复计算子问题,用数组保存已经计算好的结果

 class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int nums[n+]; //nums[i]表示i个节点的二叉查找树的数目
memset(nums, , sizeof(nums));
return numTreesRecur(n, nums);
}
int numTreesRecur(int n, int nums[])
{
if(nums[n] != )return nums[n];
if(n == ){nums[] = ; return ;}
int tmp = (n>>);
for(int i = ; i <= tmp; i++)
{
int left,right;
if(nums[i-])left = nums[i-];
else left = numTreesRecur(i-, nums);
if(nums[n-i])right = nums[n-i];
else right = numTreesRecur(n-i, nums);
nums[n] += left*right;
}
nums[n] <<= ;
if(n % != )
{
int val;
if(nums[tmp])val = nums[tmp];
else val = numTreesRecur(tmp, nums);
nums[n] += val*val;
}
return nums[n];
}
};

非递归代码:从0个节点的二叉查找树数目开始自底向上计算,dp方程为nums[i] = sum(nums[k-1]*nums[i-k]) (k = 1,2,3...i)

 class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int nums[n+]; //num[i]表示i个节点的二叉查找树数目
memset(nums, , sizeof(nums));
nums[] = ;
for(int i = ; i <= n; i++)
{
int tmp = (i>>);
for(int j = ; j <= tmp; j++)
nums[i] += nums[j-]*nums[i-j];
nums[i] <<= ;
if(i % != )
nums[i] += nums[tmp]*nums[tmp];
}
return nums[n];
}
};

LeetCode:Unique Binary Search Trees II

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

For example,
Given n = 3, your program should return all 5 unique BST's shown below.

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

按照上一题的思路,我们不仅仅要保存i个节点对应的BST树的数目,还要保存所有的BST树,而且1、2、3和4、5、6虽然对应的BST数目和结构一样,但是BST树是不一样的,因为节点值不同。

我们用数组btrees[i][j][]保存节点i, i+1,...j-1,j构成的所有二叉树,从节点数目为1的的二叉树开始自底向上最后求得节点数目为n的所有二叉树                                                                               本文地址

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<vector<TreeNode*> > > btrees(n+, vector<vector<TreeNode*> >(n+, vector<TreeNode*>()));
for(int i = ; i <= n+; i++)
btrees[i][i-].push_back(NULL); //为了下面处理btrees[i][j]时 i > j的边界情况
for(int k = ; k <= n; k++)//k表示节点数目
for(int i = ; i <= n-k+; i++)//i表示起始节点
{
for(int rootval = i; rootval <= k+i-; rootval++)
{//求[i,i+1,...i+k-1]序列对应的所有BST树
for(int m = ; m < btrees[i][rootval-].size(); m++)//左子树
for(int n = ; n < btrees[rootval+][k+i-].size(); n++)//右子树
{
TreeNode *root = new TreeNode(rootval);
root->left = btrees[i][rootval-][m];
root->right = btrees[rootval+][k+i-][n];
btrees[i][k+i-].push_back(root);
}
}
}
return btrees[][n];
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3448569.html

LeetCode:Unique Binary Search Trees I II的更多相关文章

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

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

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

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

  3. leetcode -day28 Unique Binary Search Trees I II

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

  4. LeetCode - Unique Binary Search Trees II

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

  5. Leetcode: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 ...

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

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

  7. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

  8. LeetCode——Unique Binary Search Trees II

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

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

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

随机推荐

  1. Android中ListView 控件与 Adapter 适配器如何使用?

    一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...

  2. win7开启休眠功能

    win7有的系统默认关机选项没有休眠功能,其实是没打开. cmd-> powercfg -hibernate on   即可

  3. Linux磁盘、目录、文件操作命令

    0x01. Linux磁盘分区与目录结构 ① 主分区.拓展分区.逻辑分区:早期主引导扇区MBR用64B存放主分区信息,每个分区用16B,因而上限为4个主分区,后来,因分区需求,引入拓展分区(类主分区) ...

  4. INFORMATICA 的调优之 INFORMATICA SERVER TUNING

    INFORMATICA SERVER的调优我认为主要从两个级别来做,一个是MAPPING级别,一个是SESSION级别. 对于MAPPING级别的调优: 一  对MAPPING数据流向的优化: 1 控 ...

  5. (二)我的Makefile学习冲动&&编译过程概述

    前言 一 年轻的冲动 二 学习曲线 1 Makefile基本语法 2 bash基础 3 world 三 编译过程概述 1 主机预装工具 2 编译host工具 3 编译交叉工具链 4 编译内核模块 5 ...

  6. Git操作指令进阶

    注意: 学习前请先配置好Git客户端 相关文章:Git客户端图文详解如何安装配置GitHub操作流程攻略 官方中文手册:http://git-scm.com/book/zh GIT 学习手册简介 本站 ...

  7. 备忘:文本编辑器(z.B. Sublime Text 2)策略,git策略

    1.以Sublime Text 2 为例: 新建一个test.py文件,敲完例程 代码 之后,再另存为比如 if.py, list_tuple.py云云 而test.py可以一直用来编辑 2.git ...

  8. django中的站点管理

    所谓网页开发是有趣的,管理界面是千篇一律的.所以就有了django自动管理界面来减少重复劳动. 一.激活管理界面 1.django.contrib包 django自带了很多优秀的附加组件,它们都存在于 ...

  9. DAC使用基本准则

    DAC Nyquist Zones, Zero Order Hold, and Images why do the Fout images exist in every Nyquist zone? W ...

  10. 分享十二个有用的jQuery代码

    分享7个有用的jQuery代码 这篇文章主要介绍了7个有用的jQuery技巧分享,本文给出了在新窗口打开链接.设置等高的列.jQuery预加载图像.禁用鼠标右键.设定计时器等实用代码片段,需要的朋友可 ...