95. Unique Binary Search Trees II (Tree; DFS)
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
思路:结果要保留树的所有节点,所以每次都new出新节点。
new新节点的顺序是从下往上(否则在遍历到子节点的时候,还得深度拷贝所有的父辈节点),所以要先访问左、右儿子,再访问根节点=>后序遍历。
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
vector<TreeNode*> result;
if(n==)
{
return result;
}
postOrderTraverse(,n, result);
return result;
}
void postOrderTraverse(int start, int end, vector<TreeNode*> &rootArray)
{
if(start == end){ //递归结束条件:碰到叶子节点(没有子节点)
rootArray.push_back(new TreeNode(start));
return;
}
for(int i = start; i<=end; i++){ //iterate all roots
vector<TreeNode*> leftTree;
vector<TreeNode*> rightTree;
if(i > start){ //build left tree
postOrderTraverse(start, i-, leftTree);
}
if(i < end){ //build right tree
postOrderTraverse(i+, end, rightTree);
}
//visit root: build a new tree for each (leftTree, rightTree) pair
if(leftTree.empty()){
for(int j = ; j< rightTree.size(); j++){
TreeNode* root = new TreeNode(i);
root->right = rightTree[j];
rootArray.push_back(root);
}
}
else if(rightTree.empty()){
for(int j = ; j< leftTree.size(); j++){
TreeNode* root = new TreeNode(i);
root->left = leftTree[j];
rootArray.push_back(root);
}
}
else{
for(int j = ; j< leftTree.size(); j++)
{
for(int k = ; k< rightTree.size(); k++)
{
TreeNode* root = new TreeNode(i);
root->left = leftTree[j];
root->right = rightTree[k];
rootArray.push_back(root);
}
}
}
}
}
};
95. Unique Binary Search Trees II (Tree; DFS)的更多相关文章
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [leetcode tree]95. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- leetcode 95 Unique Binary Search Trees II ----- java
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
随机推荐
- ES选主策略
ES版本5.6.3 1.整个流程的开始,实在node启动后触发的,Node.java中start()方法,通过调用ZenDiscovery.java中的doStart()方法,之后会调用startIn ...
- L180
The cylinder is a crucial part of the washing machine His debonair dismissal of my inquiry concernin ...
- Sqlserver 按照时间段统计数据
WITH t1 ( [hour], title ) , ' 0:00:00--- 1:00:00' UNION ALL , ' 1:00:00--- 2:00:00' UNION ALL , ' 2: ...
- Python数据类型-01.数字和布尔值
本节主要介绍Python中的基础知识中的数据类型,数字和布尔值 介绍几个知识点:1)内置函数print()的用法,直接打印括号里面的内容,或者print后跟多个输出,以逗号分隔.2)内置函数type( ...
- 手机dp和px的转换
1dp 0.75px ----> 320*240 1dp 1px ----->480*320 1dp 1.5px ----->800*480 4 ...
- 6-1 Deque(25 分)Data Structures and Algorithms (English)
A "deque" is a data structure consisting of a list of items, on which the following operat ...
- POI2015题解
POI2015题解 吐槽一下为什么POI2015开始就成了破烂波兰文题目名了啊... 咕了一道3748没写打表题没什么意思,还剩\(BZOJ\)上的\(14\)道题. [BZOJ3746][POI20 ...
- 【转】Windows消息投递流程:一般窗口消息投递(WM_LBUTTONCLICK)
原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182646 本例通过在单文档程序的视图中添加WM_LBUTTONCLICK消息处理函数 ...
- Some index files failed to download. They have …… or old ones used instead
问题: 平台:Ubuntu12.04 W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/precise/universe/bin ...
- Autofac log4net Integration Module
log4net Integration Module While there is no specific assembly for log4net support, you can easily i ...