【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 ...
随机推荐
- 想精度高,可以考虑用c语言中的函数gettimeofday
大家好: 在 win32 + bcb 时, 有个 GetTickCount() 返回第统启动到现在的 tick, 单位 ms.请问在 Linux + qt5 怎样实现呢? 如果用 QDateTime ...
- 文件的哈希值不在指定的目录文件中。此文件可能已损坏或被篡(Windows10 /Windows8.1)
------------------------------------------Windows10------------------------------------------------ ...
- HDU 3231 Box Relations
题目大意: 给定一些正方体的关系,要求一组符合这些关系的正方体坐标,如果不存在符合条件的正方体坐标,IMPOSSIBLE.(Special Judge) 实力还是太弱了,完全不会…… #include ...
- rpc的学习
rpc(Remote process call 即远程过程调用)是一种请求-相应的协议, 主要使用于C/S架构中,使得分布式系统成为可能.由客户端发起请求,服务端调用各种参数处理请求,当服务器在处理请 ...
- hdu1715 大菲波数
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1715 Problem ...
- iOS开展-CocoaPods安装和使用教程
原文链接: iOS开展-CocoaPods安装和使用教程 修正已经增加了自己的理解. CocoaPods安装和使用教程 Code4App 原创文章.转载请注明出处:http://code4app.co ...
- c++ 输出虚函数表内容
class Base{ public: virtual void f(){cout<<"Base::f"<<endl;} virtual void g(){ ...
- CodeFirst-Section1之小例子
尽可能做到不说一些晦涩难懂的语言,Follow Me...... 环境:Visual Studio 2013+.Net Framework 4.5 1.什么是Code First? 说白了就是先建好C ...
- c#中使用log4net工具记录日志
首先,去官网下载log4net工具 链接http://logging.apache.org/log4net/download_log4net.cgi 目前最新的版本 log4net-1.2.15-bi ...
- vim: 字符串替换
:s/str1/str2/ 替换当前行第一个 str1 为 str2:s/str1/str2/g 替换当前行所有 str1 为 str2 ( 注意, s/xx/xxx/g 语句从s开始,中间的空格视 ...