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 ...
随机推荐
- 010-JDK可视化监控工具-VisualVM
一.概述 VisualVM是一个集成多个JDK命令行工具的可视化工具.VisualVM基于NetBeans平台开发,它具备了插件扩展功能的特性,通过插件的扩展,可用于显示虚拟机进程及进程的配置和环境信 ...
- const,var,let区别(转载)
1.const定义的变量不可以修改,而且必须初始化. const b = 2;//正确 // const b;//错误,必须初始化 console.log('函数外const定义b:' + b);// ...
- Python基础-面向对象2
一.成员修饰符 共有成员 私有成员:创建方式在成员之前加两个下划线,私有成员无法直接访问,只能间接访问 子类不能继承父类的私有属相私有普通字段的访问方式: class Fansik: def __in ...
- C# 创建单例你会几种方式?
关于为什么需要创建单例?这里不过多介绍,具体百度知. 关于C# 创建单例步骤或条件吧 1.声明静态变量:2.私有构造函数(无法实例化)3.静态创建实例的方法:至于我这里的Singleton是seal ...
- jquery jsonp请求错误处理
jQuery将ajax方法进行了封装,jsonp可以轻松实现跨域请求. $.ajax({type: 'GET', url: "<%= SysUtils::TASK_CENTER_URL ...
- selenium破解数字验证码
搞了半天,总算弄出来了,识别率还可以,普通的数字验证码 from selenium import webdriver from PIL import Image import pytesseract ...
- Linux开发工具教程
今天把上个星期写的Linux开发工具相关的教程整理一下,方便阅读: 1.第一课 GCC入门: 2.第二课 GCC入门之静态库以及共享库: 3.第三课 Makefile文件的制作(上) : 4.第四课 ...
- 《Spring Boot 实战》随记
第一部分 Spring 4.x 1. Spring基础 略过 2. Spring常用配置 2.1 Bean的scope 使用@Scope注解配置scope.默认signleton,原型模式protot ...
- vue-cli的utils.js文件详解
转载自:http://www.cnblogs.com/ye-hcj/p/7078047.html utils.js文件 // 引入nodejs路径模块var path = require('path' ...
- 什么是jstack
以下是百度百科的内容 jstack是java虚拟机自带的一种堆栈跟踪工具. jstack用于生成java虚拟机当前时刻的线程快照.线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成 ...