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. 启动Eclipse时发生An internal error occurred during: "Initializing Java Tooling"错误

    详细提示如下: An internal error occurred during: "Initializing Java Tooling". Illegal exception ...

  2. 转 解决linux下tomcat的shutdown命令杀不死进程

    tomcat在windows下可以直接关闭,但是貌似在Linux下有时候shutdown.sh 没有关闭tomcat进程; 现象:在Linux下shutdown.sh ,然后查看tomcat进程发现没 ...

  3. iOS UI-九宫格

    第一节课: .复习 .运行App应用管理,简单界面分析 .一个应用为一个整体,直接创建一个appView然后计算frame .说明弊端,应该根据数据的个数来for循环创建 第二节课: .加载plist ...

  4. linux thtree level page tables

    To translate a virtual address into a physical one, the CPU must take the contents of each level fie ...

  5. jquery条形码生成器

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  6. 2017广东工业大学程序设计竞赛决赛 Problem E: 倒水(Water) (详解)

    倒水(Water) Description 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水 ...

  7. DevExpress v17.2新版亮点——VCL篇(二)

    用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress VCL v17.2 的新功能,快来下载试用新版本! DPI ...

  8. addslash()

    php addslashes函数的作用是在预定义的字符前面加上反斜杠,这些预定义字符包括: 单引号(') 双引号(") 反斜杠(\) NULL addslashes函数经常使用在向数据库插入 ...

  9. 强大的Grafana worldping插件

    安装worldping插件: 官方插件地址 查看安装说明,在grafana server上执行命令,完成后重启grafana server,重启,请注意,要重启 重启grafana service g ...

  10. 在linux中使用终端浏览器w3m

    w3m是一个基于文本的网页浏览器,支持多种操作系统,在命令行终端可以很好的支持中文.即使在没有鼠标支持的情况下也可以检查网页的输出. 1. 安装w3m $ sudo apt install w3m 2 ...