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. 201621123006 《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  2. Python基础学习----异常

    ''' 异常: 程序在运行的时候,Python的解释器遇到一个错误会停止运行. 并且会提供错误的信息,这就是异常 抛出异常: 程序停止执行并且提示错误信息这个动作就是抛出异常(raise Except ...

  3. 关于VC中LineDDA函数的调用

    在项目里碰到这个函数,不知道怎么使用,记录在这里. 该函数的原型如下: BOOL LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd, LIN ...

  4. WIN7下用笔记本创建无线网

    系统要求:WIN7  硬件要求:拥有无线网卡  网络要求:已经连接internet 1.启用并设定虚拟Wifi网卡:运行以下命令启用虚拟无线网卡(相当于打开路由器): netsh wlan set h ...

  5. test20181016 B君的第三题

    题意 B 君的第三题(haskell) 题目描述 大学四年,我为什么,为什么不好好读书,没找到和你一样的工作. B 君某天看到了这样一个题,勾起了无穷的回忆. 输入\(n, k\) 和一棵\(n\) ...

  6. StreamSets 部署 Pipelines 到 SDC Edge

    可以使用如下方法: 下载edge 运行包并包含pipeline定义文件. 直接发布到edge 设备. 在data colelctor 机器配置并配置了edge server 地址(主要需要网络可访问) ...

  7. 给Linux内核增加一个系统调用的方法(转)

    作者:chenjieb520 给Linux内核增加一个系统调用的方法    为了更加好地调试linux内核,笔者的实验均在mini6410的arm板上运行的.这样做的原因,第一是因为本人是学嵌入式的, ...

  8. mysql中去重 distinct 用法

    在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...

  9. ETL流程概述及常用实现方法

    ETL流程概述及常用实现方法 http://blog.csdn.net/btkuangxp/article/details/48224187 目录(?)[-] 1抽取作业 1手工开发抽取作业时候的常用 ...

  10. vim配置之安装脚本

    vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...