LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector:
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
树节点的定义是下面这样的
/**
* Definition for a binary tree node.
* 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) {
return createNode(, n);
} vector<TreeNode*> createNode(int start, int end)
{
vector<TreeNode*> result;
if(start > end){
result.push_back(NULL);
return result;
}
for(int i = start; i <= end; ++i){
vector<TreeNode*> leftNode = createNode(start, i - );
vector<TreeNode*> rightNode = createNode(i + , end);
for(int j = ; j < leftNode.size(); ++j){
for(int k = ; k < rightNode.size(); ++k){
TreeNode * tmpNode = new TreeNode(i);
tmpNode->left = leftNode[j];
tmpNode->right = rightNode[k];
result.push_back(tmpNode);
}
}
}
return result;
}
};
这一题实际上更另外一个叫做different ways to add parentheses的题目比较相似,这个详见上一篇博文。
java版本的代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<TreeNode> generateTrees(int n) {
if(n == )//如果不加上这一条,当为0的时候会返回[[]],不知道为什么,很奇怪
return new ArrayList<TreeNode>();
return createTree(, n);
} public List<TreeNode> createTree(int start, int end){
ArrayList<TreeNode> result = new ArrayList<TreeNode>();
if(start > end){
result.add(null);
return result;
}
for(int i = start; i <= end; ++i){
for(TreeNode leftNode : createTree(start, i - )){
for(TreeNode rightNode : createTree(i + , end)){
TreeNode node = new TreeNode(i);
node.left = leftNode;
node.right = rightNode;
result.add(node);
}
}
}
return result;
}
}
LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)的更多相关文章
- [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 ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 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 ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode OJ——Unique Binary Search Trees II
http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码, ...
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
随机推荐
- 思考在伟大的互联网世界中,我是谁?——By Me in 2016
互联网伟大在哪里? 互联网的发明是不是伟大的,这个问题就如同这个世界上许许多多的问题一样,很大程度上取决于人们不同的经历.不同的见识,乃至不同的信念.不同的人生态度. 摘录网上的一段表述:“互联网(产 ...
- MAC下配置MAVEN环境变量配置
MAVEN环境变量的配置: 第一步:在MAVEN的官网下载MAVEN.http://maven.apache.org/download.cgi,我这里下载的是apache-maven-3.39-bin ...
- s5_day9作业
# 1 编写 tail -f a.txt |grep 'error' |grep '404'命令,周一默写 # import time # def tail(filepath,encoding='ut ...
- kmp模板 && 扩展kmp模板
kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...
- POJ - 2226 Muddy Fields (最小顶点覆盖)
*.*. .*** ***. ..*. 题意:有一个N*M的像素图,现在问最少能用几块1*k的木条覆盖所有的 * 点,k为>=1的任意值. 分析:和小行星那题很像.小行星那题是将一整行(列)看作 ...
- C++添加简单的日记记录
#include<fstream>#include<iostream> using namespace std;//这是一种日记记录 b 种void LOG(char *tx, ...
- poj2993
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...
- jQuery二级下拉菜单
在线演示 本地下载
- Spring Boot JDBC 连接数据库
文本将对在Spring Boot构建的Web应用中,基于MYSQL数据库的几种数据库连接方式进行介绍. 包括JDBC.JPA.MyBatis.多数据源和事务. JDBC 连接数据库 1.属性配置文件( ...
- Spring_通过 FactoryBean 配置 Bean
beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...