1、



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

分析:本题非常easy想到用递归的方法,依次将每一个点作为根结点,然后递归左右子树,可是在这个递归中有个问题,怎样推断递归结束,怎样保留上层的结点,怎样推断递归的是左子树还是有子树,递归的写法还是非常重要的。本题中,採用每次先求得下层子树左右子树的结点,然后依据左右子树的不同情况形成不同的树,保存这些树的根结点。

代码例如以下:

class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return constructTree(0,n-1);
}
vector<TreeNode*> constructTree(int startIndex, int endIndex){
vector<TreeNode *> result; //保存下一层的结点
if(startIndex > endIndex){
result.push_back(NULL);
return result;
}
for(int i=startIndex; i<=endIndex; ++i){
vector<TreeNode*> leftTrees = constructTree(startIndex,i-1);
vector<TreeNode*> rightTrees = constructTree(i+1,endIndex);
for(int j=0; j<leftTrees.size(); ++j){
for(int k=0; k<rightTrees.size(); ++k){
TreeNode * newNode = new TreeNode(i+1);
result.push_back(newNode);
newNode->left = leftTrees[j];
newNode->right = rightTrees[k]; }
}
}
return result;
}
};

2、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

分析:先做上面的题,后面这个题就简单了,採用递归,每次计算下层的左子树和右子树的数目,本层的不同树的个数为左子树的数目与右子树的数目相乘,每种不同的情况相加就可以。

class Solution {
public:
int numTrees(int n) {
if(n < 1){
return 1;
}
return numTrees(1,n);
}
int numTrees(int startIndex, int endIndex){
int num = 1;
if(startIndex > endIndex){
return num;
}
num = 0;
for(int i=startIndex; i<=endIndex; ++i){
int leftNum = numTrees(startIndex,i-1);
int rightNum = numTrees(i+1,endIndex);
num += leftNum * rightNum;
}
return num;
}
};

leetcode -day28 Unique Binary Search Trees I II的更多相关文章

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

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

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

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

  4. [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II

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

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

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

  6. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  7. 【leetcode】Unique Binary Search Trees II

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

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

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

  9. 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]* ...

随机推荐

  1. Mysql批量导入约束报错

    SET foreign_key_checks = 0;   禁用外键,在文件顶部加 SOURCE dump_file_name;      进行SQL执行 SET foreign_key_checks ...

  2. Service Account和其secrets 作用和场景,看了不亏。。

    Service Account概念的引入是基于这样的使用场景: 运行在pod里的进程需要调用Kubernetes API以及非Kubernetes API的其它服务.Service Account它并 ...

  3. Hibernate---hbm2ddl和数据库方言的配置

    Hibernate---hbm2ddl和数据库方言的配置 hibernate配置文件--hbm2ddl.auto属性值的区别: update: 最常用的取值,如果但其数据库中不存在表结构,那么自动创建 ...

  4. 对称点line

    2 线 题⽬描述 有一天rax看到了男神,有时可爱美丽的她派ypq去把yyqx抓回来给rax欣赏,但ypq和yyqx间隔了一条线,她必须跳到yyqx以前待得点的对称点才有可能抓到yyqx给出⼀条直线, ...

  5. OC 设计模式

    设计模式 一种或几种被所有程序员广泛认同的,有某些特定功能,或者实现某些特殊作用的编码格式 单例模式 键值编码(KVC) 键值观察(KVO) 观察者模式() 工厂模式(工厂方法) ps:MVC &am ...

  6. PHP:第一章——PHP中逻辑运算符的使用方法

    //逻辑运算符 $a=;$b=;$c=;$d=; //逻辑与(and 和 &&)他们两个的逻辑是一样的,如果两个值都为true,结果才为true,否则是false. //var_dum ...

  7. 自定义oncontextmenu

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. sgu 125 Shtirlits dfs 难度:0

    125. Shtirlits time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a checkered fi ...

  9. ipython与sublime调用其shell出现的问题

    本机电脑 win10 已安装python3.5 1.    直接在命令行运行 pip install ipython[all] 安装 ipython 安装完成后 在命令行输入 jupyter note ...

  10. iOS 9 通用链接(Universal Links)

    什么是Universal Links? 在iOS9之前,对于从各种从浏览器.Safari中唤醒APP的需求,我们通常只能使用scheme.但是这种方式需要提前判断系统中是否安装了能够响应此scheme ...