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)的更多相关文章

  1. 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]* ...

  2. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  3. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  4. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  5. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 第十一次作业 - Alpha 事后诸葛亮

    拖鞋旅游队团队事后诸葛亮会议 前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10054510.html 时间:2018-12-1 20:00 地 ...

  2. js:关闭当前页面

    var userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") != -1 || userAgent.i ...

  3. HDU 2050:折线分割平面

    折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. SqlServer高级特性--游标

    游标 用途:在数据很多的时候,如果在java代码中进行循环之后再进行更新数据,会造成频繁的连接数据库,耗费性能,所以就可以使用到游标 作用:查询出来的集合直接在SQL中进行遍历在进行更新 DECLAR ...

  5. CSS基础知识,学前准备

    1.引入层叠样式表: A.行内引入 <bodystyle="background-color:#cccccc">; 在标签内使用style属性 </body> ...

  6. Linux下sniffer实现(转)

    转发网址:https://blog.csdn.net/eqiang8271/article/details/8489769 //Example 1. #include <stdio.h> ...

  7. 增强型for和Iterator学习

    1,增强for和对于非集合类(没有实现 Iterable接口)的数组遍历效果一样 2,对于集合类,就是隐式调用迭代器 iterator的遍历,各有各个场合 3,对于arraylist来所,由于数据结构 ...

  8. CentOS 6.5 下keepalived服务的配置

    CentOS 6.5 下keepalived服务的配置 参考网站: http://zhangxugg-163-com.iteye.com/blog/1665419 http://www.2cto.co ...

  9. mkdir -p 多层次目录创建

    mkdir的-p选项允许你一次性创建多层次的目录,而不是一次只创建单独的目录.例如,我们要在当前目录创建目录Projects/a/src,使用命令 1 mkdir -p Project/a/src 而 ...

  10. python 下载图片的方法

    a='http://wx1.sinaimg.cn/mw600/006HOayNgy1fqjdi2nxohj32pw3o8x6s.jpg'  #图片下载地址   ( 这里改成 文件txt地址)w='/U ...