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. SharePoint 2013的100个新功能之网站管理(三)

    一:跨网站发布 跨网站发布是SharePoint 2013的一个新功能,可以使用户跨网站集.Web应用程序甚至是场重用内容.该功能叫做跨网站发布功能.用户可以使用该功能在SharePoint 2013 ...

  2. react native遇到的坑

    1.模拟器报错no bundle url present https://github.com/facebook/react-native/issues/12754 http://www.cnblog ...

  3. IE9(8)跨域(CORS)解决方案

    HTML5中 XMLHttpRequest Level 2 的推出.可以通过在返回的HTTP请求头中加入 Access-Control-Allow-Origin 的设置,让浏览器支持对不同域的AJAX ...

  4. CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)

    You are a given a list of integers a 1 ,a 2 ,…,a n  a1,a2,…,an and s s of its segments [l j ;r j ] [ ...

  5. 6-8 Percolate Up and Down(20 分)

    Write the routines to do a "percolate up" and a "percolate down" in a binary min ...

  6. git操作提交方式

    git代码提交 第一次提交代码 在本地建立一个文件夹用来存储代码,相当于一个仓库进入文件夹目录输入下面命令 echo "# xxx" >> README.md (添加一 ...

  7. USB gadget学习笔记

    1.usb-OTG-ADP-HNP-SRP https://blog.csdn.net/xiongjiao0610/article/details/44150849

  8. opencrud 中文参考翻译(完成部分)

    opencrud 是一个就比较好的关于graphql 实现的指南(当前只有部分文档,完整的还没有,实际上apollo 有相关的文档都挺不错的) 同时在github 有一个中文的简单翻译(后期应该会和官 ...

  9. NTP时间服务器的配置

      1.NTP简介NTP(Network Time Protocol,网络时间协议)是用来使网络中的计算机,时间同步的一种协议.NTP服务器利用NTP协议来提供时间同步服务. 2 .环境准备主机名   ...

  10. hadoop YARN配置参数剖析—MapReduce相关参数

    MapReduce相关配置参数分为两部分,分别是JobHistory Server和应用程序参数,Job History可运行在一个独立节点上,而应用程序参数则可存放在mapred-site.xml中 ...