题目如下所示:返回的结果是一个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(唯一二叉搜索树)的更多相关文章

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

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

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

  3. [Leetcode] 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] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  5. LeetCode OJ:Unique Binary Search Trees(唯一二叉搜索树)

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

  6. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

  7. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. LeetCode OJ——Unique Binary Search Trees II

    http://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 一题要求得出所有树的种类数,二题要求得出所有树. 在一题的基础上修改代码, ...

  9. Leetcode96.Unique Binary Search Trees不同的二叉搜索树

    给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...

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

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

随机推荐

  1. 005-jdk安装卸载

    一.yum安装 1.查看CentOS自带JDK是否已安装. yum list installed |grep java 2.若有自带安装的JDK,卸载CentOS系统自带Java环境 卸载JDK相关文 ...

  2. SVG Use(转)

    转自:http://www.zhangxinxu.com/wordpress/2014/07/introduce-svg-sprite-technology/ 未来必热:SVG Sprite技术介绍 ...

  3. 利用WebBrowser实现自动登入功能

    公司内部改革,对考勤方面做出调整,要求实现办公自动化,在OA进行上下班考勤:作为程序员,突发奇想如何实现自动化考勤应用? 需求如下: 可设置考勤地址.用户信息.上下班时间: 根据设置的上下班时间,定时 ...

  4. 单文件快速体验使用react输出hello_world

    看了下react官方的hello world教程, 感觉对新手很不友好.codepen虽然好用, 但是封装太多东西, 看起来 太抽象. 还是喜欢像学习jQuery那样, 直接在单文件中引入必要的js文 ...

  5. IIS Internet Information Service

    Visual Studio 和 visio 都有的Web服务,IIS 发布的时候,直接可以用本机的IIS进行发布,Windos自带有Web服务,只需要配置一下,然后配上域名就OK了,简直太方便了 来自 ...

  6. 微服务与SOA

    微服务跟SOA有什么区别呢,可以把微服务当做去除了ESB的SOA.ESB是SOA架构中的中心总线,拓扑结构应该是星形的,而微服务是去中心化的分布式软件架构. 一.巨石(monolith) web应用程 ...

  7. 微信小程序快速开发

    微信小程序快速开发 一.注册小程序账号,下载IDE 1.官网注册https://mp.weixin.qq.com/,并下载IDE. 2.官方文档一向都是最好的学习资料. 注意:1)注册账号之后会有一个 ...

  8. 配置OpenVpn使用证书和用户名密码双验证

    修改OpenVpn的主配置文件 添加一下内容: [root@check1 openvpn]# tail -3 server.conf auth-user-pass-verify /etc/openvp ...

  9. Java程序执行Linux命令(JSP运行其他程序)

    java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...

  10. Oracle imp 导入数据出现 ORA-12560

    错误如下: D:\software\xfwebdb2015-05-11\autobackup>imp Import: Release 10.2.0.1.0 - Production on 星期三 ...