Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
示例:
输入: 3
输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3], [1,null,2,null,3] ]
重建树一般都是递归求解
class Solution {
public:
vector<TreeNode*> generateTrees(int n)
{
if(n == 0)
return {};
return GetAns(1, n);
}
vector<TreeNode*> GetAns(int start, int end)
{
vector<TreeNode*> subTree;
if(start > end)
{
subTree.push_back(NULL);
}
else
{
for(int i = start; i <= end; i++)
{
vector<TreeNode*> left = GetAns(start, i - 1);
vector<TreeNode*> right = GetAns(i + 1, end);
for(auto l : left)
{
for(auto r : right)
{
TreeNode *root = new TreeNode(i);
root ->left = l;
root ->right = r;
subTree.push_back(root);
}
}
}
}
return subTree;
}
};
Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2的更多相关文章
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 40.Unique Binary Search Trees(不同的二叉搜索树)
Level: Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...
- LeetCode-95. Unique Binary Search Trees II
Description: Given n, generate all structurally unique BST's (binary search trees) that store values ...
- leetcode95 Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 41. Unique Binary Search Trees && Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
随机推荐
- openwrt xfrp移植
对开源软件表示支持 https://github.com/KunTengRom/xfrp 上传编译,选择 cp .config xxx make 刷机 客户端配置文件: /tmp/etc# cat x ...
- 构造流量图+乱搞——cf990F
/* 结论1:有解的充要条件是所有点权之和为0 结论2:删掉环上的一条边,只要将这个环上的其余边都减去这条边的边权,那么这个图仍是等价的 从原图网络中构造出一棵带权值的树即可,其他边权都设置为0 通过 ...
- 线性推概率——cf1009E好题!
依次求每一段公里的期望消耗即可,这是可以递推的 dp[i]表示每公里的期望消耗 dp[i]=1/2*a1+1/4*a2 +...+1/2^(i-1)*ai-1 + 1/2^(i-1)*ai注意最后一项 ...
- Myeclipse从外部导入项目时,jsp和html页面中所有的onclick="return xx()"位置均出现cannot return from outside function() or method()错误
- BCB编写DLL终极手册
一. 编写 DLL File/New/Dll 生成 Dll 的向导,然后能够添加导出函数和导出类 导出函数:extern "C" __declspec(dllexport) Exp ...
- 玩转gulp之gulp编译less
用好gulp grunt webpack让前端编程走向自动化,是作为一个前端开发必须学会的技能,不然逼格怎么提升的上去呢... 然后教大家如何用gulp装逼.一点点的学,都是相通的嘛 1. 安装nod ...
- iOS开发之SceneKit框架--SCNScene.h
1.SCNScene SCNScene是一个场景图——具有附加几何形状.光照.摄像机和其他属性的节点的层次结构,共同形成可显示的3D场景. 2.相关API简介 初始化方法 //懒加载 + (insta ...
- 使用Math.random()函数生成n到m间的随机数字
使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备,Math.random()函数返回0和1之间的伪随机数 讲解: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后期的j ...
- 小程序swiper-item内容过多显示不全的解决方案
最近在项目遇到swiper高度不能自适应,导致swiper-item 里面的内容过多时只能显示一部分,最终解决方案:<swiper current="{{currentTab}}&qu ...
- leetcode-213-打家劫舍二
题目描述: 方法一: class Solution(object): def rob(self, nums): """ :type nums: List[int] :rt ...