【Leetcod】Unique Binary Search Trees II
给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树。
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 binary tree
* 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 generateTrees(1,n);
} vector<TreeNode *> generateTrees(int start, int end)
{
vector<TreeNode *> trees;
if (start > end)
{
trees.push_back(NULL);
return trees;
}
if (start==end)
{
trees.push_back(new TreeNode(start));
return trees;
} for (int i=start; i<=end; ++i)
{
vector<TreeNode *> treesleft = generateTrees(start,i-1);
vector<TreeNode *> treesright = generateTrees(i+1,end); for (size_t j=0; j<treesleft.size(); ++j)
{
for (size_t k=0; k<treesright.size(); ++k)
{
TreeNode *root = new TreeNode(i);
root->left = treesleft[j];
root->right = treesright[k];
trees.push_back(root);
}
}
} return trees;
}
};
【Leetcod】Unique Binary Search Trees II的更多相关文章
- 【leetcode】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 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【leetcode】 Unique Binary Search Trees II (middle)☆
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【树】Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【Leetcode】【Medium】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
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【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 (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【题解】【BST】【Leetcode】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- HDOJ 1043 Eight(A* 搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路分析: <1> 搜索算法: A*算法, Heuristic函数:曼哈顿距离 &l ...
- Hierarchical Storage structure
1.hierarchical storage structure This notion of inserting a smaller, faster storage device (e.g ...
- U盘安装Ubuntu14.4时遇到分区问题记录
1.在安装Ubuntu14.4时,遇到如果先分出 / 跟挂载的主分区时,后面只能再分一个swap,或者挂载一个/home,或者一个/ boot 时不能继续分区,当然想安装也是不能不能成功的. 解决办法 ...
- React Native-目前最火的前端技术?
做为一名产品经理,你是否遇到过这样的窘境,“帮我把字体调成 16号呗,颜色变成 #FFFF00FF,老大说这里最好改一下”,作为一名 app 的开发只能无奈但心里窃喜的告诉你,“只能等下个版本了,必须 ...
- Visual Studio Tools for Unity安装及使用
Visual Studio Tools for Unity安装及使用 转载自:CSDN 晃了一下,10.1到现在又过去两个月了,这两个月什么也没有学,整天上班下班,从这周末开始拾起unity,为了年后 ...
- 解决gnuplot中'Terminal type set to 'unknown'不能显示绘图的问题
安装gnuplot: sudo apt-get install gnuplot 安装成功后,在终端输入gnuplot,进入gnuplot. 直接进行一个小测试: plot sin(x) 发现不能显示绘 ...
- STL之stack(栈)
栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...
- TortoiseSVN和Eclipse中subversion无法协同使用
环境: Eclipse版本Luna, 第一次安装subversion插件时, 使用了http://download.eclipse.org/releases/luna中的Subversive, 版本为 ...
- JS拖动浮动DIV
<!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>j ...
- Iframe知识点
var oIframe=document.getElementById('iframe1'); 获取iframe对象: oIframe.contentWindow.// iframe 里的win ...