LeetCode----Unique Binary Search Trees 2
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
分析:
递归即可。枚举所有的 root 节点,root 的左子树和右子树就是两个子问题,递归求解。
但是有一点要注意,容易出错。
如果子树的节点个数为零,这时候返回的应该是一个包含 NULL 的 vector;
/**
* 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) {
vector<TreeNode*> res;
if(n == 0){
res.push_back(NULL);
return res;
}
return generateTrees(1, n);
}
private:
vector<TreeNode *> generateTrees(int left, int right)
{
TreeNode *node(NULL);
vector<TreeNode*> trees, left_tree, right_tree;
if(left > right)
{
trees.push_back(NULL);
return trees;
}
for(int i=left; i<=right; ++i)
{
left_tree = generateTrees(left, i-1);
right_tree = generateTrees(i+1, right);
for(auto j : left_tree)
for(auto k : right_tree)
{
node = new TreeNode(i);
node->left = j;
node->right = k;
trees.push_back(node);
}
}
return trees;
}
};
LeetCode----Unique Binary Search Trees 2的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [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 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- Leetcode: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 @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode: Unique Binary Search Trees [095]
[题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
随机推荐
- python 基础学习(元组,if,for)
1.tuple对象 tuple 元组 有序的列表 tuple一旦创建不能修改 a.定义元组t=('a','b','c')空元素的tuple t=()()既表示tuple 也表示运算符的优先级 所以定义 ...
- WPF:ListView数据绑定及Style
DrugRecordsWin.xaml <ListView Grid.Row="4" Grid.Column="1" Name="list_Dr ...
- python获取文件的内容
- 0302IT行业就业&软件工程之我所思和所想
阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...
- 【工具推荐】ELMAH——可插拔错误日志工具
今天看到一篇文章(构建ASP.NET网站十大必备工具(2)),里面介绍了一个ELMAH的错误日志工具,于是研究了一下. ELMAH 是 Error Logging Modules and Handle ...
- MSMQ消息队列
MSMQ全称MicroSoft Message Queue,微软消息队列,是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一 ...
- web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker
namespace EWorkpal.WebApi { public class HttpNotFoundDefaultHttpControllerSelector : DefaultHttpCont ...
- ubuntu 防火墙 添加策略 解决mysql远程访问问题
ubuntu 的iptables 文件不在 init.d中 不能 service iptables restart 只修改 /etc/iptables 文件也不管用 sudo iptables -L ...
- Linux-编译器gcc/g++编译步骤
gcc和g++现在是gnu中最主要和最流行的c&c++编译器.g++是c++的命令,以.cpp为主:对于c语言后缀名一般为.c,这时候命令换做gcc即可.编译器是根据gcc还是g++来确定是按 ...
- POJ 2763
题意:给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值. #pragma comment(linker, "/STACK:1024000000,10 ...