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. DbVisualizer 连接 SQL Server 2008配置

    软件准备 1.SQLServer驱动准备,可在该连接下载:https://pan.baidu.com/s/1i4V1Ivz (1). 解压JDBC for SQLServer drive.rar,得到 ...

  2. Java中,什么时候用logger.debuge,info,error

    简单的说,就是配合log的等级过滤输出比如,你在开发的时候,要验证一个方法有没有被调用到,为了方便调试,通常会在这个方法开始的时候加一些system.out.但是项目真正发布的时候这些代码通常是要移除 ...

  3. json-patch 了解

    What is JSON Patch? JSON Patch is a format for describing changes to a JSON document. It can be used ...

  4. How To Enable EPEL Repository in RHEL/CentOS 7/6/5?

    What is EPEL EPEL (Extra Packages for Enterprise Linux) is open source and free community based repo ...

  5. win7下openvpn不能自动加路由

    在win7下用openvpn一直报这个错误,配置文件里的路由一直加不上,但是可以拔得上服务器,只好手工加路由.Thu Apr 07 23:13:51 2011 Notified TAP-Win32 d ...

  6. 简单桶排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def BucketSort(a, n): barrel = np.zeros((1, n), dtype = 'i ...

  7. Bootstrap-Plugin:附加导航(Affix)插件

    ylbtech-Bootstrap-Plugin:附加导航(Affix)插件 1.返回顶部 1. Bootstrap 附加导航(Affix)插件 附加导航(Affix)插件允许某个 <div&g ...

  8. JavaScript(二)-精简

    十三 DOM(文档对象模型) 1 作用 : 可访问javascript HTML文档的所有元素. 2 功能: (1) 改变html输出流 eg: <script>              ...

  9. 【UVA】10891 Game of Sum(区间dp)

    题目 传送门:QWQ 分析 大力dp.用$ dp[i][j] $表示$ [i,j] $A能得到的最高分 我看到博弈论就怂... 代码 #include <bits/stdc++.h> us ...

  10. Centos编译Redis4.0.9源码过程记录

    mkdir /home/redis cd /home/redis 下载源码 wget https://codeload.github.com/antirez/redis/tar/4.0.9 解压源码包 ...