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 ...
随机推荐
- Github/Eclipse管理Maven项目
Eclipse和Git插件 (To-do: 直接从workspace导入也可以,弄明白这个repo管理的本质,查看sprigmvc是如何导入的) 最新版本的Eclipse都直接集成了Git插件 Ecl ...
- Shell基础:Linux权限管理
Linux权限基本概念 查看系统(文件夹/文件)权限: ls -l =>d/- xxx xxx xxx. num owner group size date filename ...
- python中的popen和subprocess
import os from subprocess import Popen, PIPE res = os.popen('xx.exe E:\\test\\file1 E:\\test\\file2' ...
- 批量Load/Store指令的寻址方式
批量Load/Store指令用于实现在一组寄存器和一块连续的内存单元之间传输数据.也称为多寄存器寻址方式,即一条指令可以完成多个寄存器值的传送.这种寻址方式可以用一条指令最多完成传送16个通用寄存器的 ...
- RAID在数据库存储上的应用-转
随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...
- div垂直居中的问题
工作和面试时常常会遇到怎么设置div垂直居中与浏览器中:包括固定宽高和不固定宽高的 1.固定宽高的div垂直居中 宽高固定的div很容易设置让其垂直居中 <div class="cen ...
- PDF 补丁丁 0.4.1.688 测试版发布(请务必用其替换 682 测试版)
修复了测试版682 损坏书签.读取字符宽度表出错的问题.请下载了旧测试版的网友马上换用新的测试版.
- easyui layout 收缩的bug
easyui layout提供collapse方法折叠指定的 panel,'region' 参数可能的值是:'north'.'south'.'east'.'west',但是在 IE6的环境下,调用这个 ...
- 0302IT行业就业&软件工程之我所思和所想
阅读以下文章 http://www.thea.cn/news/terminal/9/9389.html http://www.shzhidao.cn/system/2015/09/22/0102610 ...
- Android在Eclipse上的环境配置
哈哈,首先转一个:https://my.oschina.net/fusxian/blog/293935 谢谢分享者 1.android SDK安装出现Failed to fetch URL http: ...