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

这次的题目要求是得到所有的树。

我的思路:

用f[n]存储1-n的所有方法的根节点

则 f[n+1] = 1作为根,f[0]做左子树,f[n]所有节点都加1做右子树  +  2作为根,f[1]做左子树,f[n - 1]所有节点都加2做右子树 +...

代码内存都没有释放,不过AC了。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; // Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
vector<vector<TreeNode *>> ans(n + , vector<TreeNode *>());
if(n == )
{
ans[].push_back(NULL);
return ans[];
} TreeNode * root = NULL;
ans[].push_back(root);
root = new TreeNode();
ans[].push_back(root);
for(int i = ; i <= n; i++) //总数字
{
for(int j = ; j < i; j++) //小于根节点的数字个数
{
for(int l = ; l < ans[j].size(); l++) //小于根节点的组成方法数
{
for(int r = ; r < ans[i - j - ].size(); r++) //大于根节点的组成方法数
{
TreeNode * root = new TreeNode(j + );
root->left = ans[j][l];
root->right = add(ans[i - j - ][r], j + ); //大于根节点的需要加上差值
ans[i].push_back(root);
}
}
}
} return ans[n];
} TreeNode * add(TreeNode * root, int Num)
{
if(root == NULL)
{
return root;
}
TreeNode * T = new TreeNode(root->val + Num);
T->left = add(root->left, Num);
T->right = add(root->right, Num); return T;
}
}; int main()
{
Solution s;
vector<TreeNode *> ans = s.generateTrees(); return ;
}

看别人的思路,把建立子树分为从数字start-end,直接递归求解,不需要像我的那样每次还要把右子树遍历增加值

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> generateTreesRec(int start, int end){
vector<TreeNode*> v;
if(start > end){
v.push_back(NULL);
return v;
}
for(int i = start; i <= end; ++i){
vector<TreeNode*> left = generateTreesRec(start, i - );
vector<TreeNode*> right = generateTreesRec(i + , end);
TreeNode *node;
for(int j = ; j < left.size(); ++j){
for(int k = ; k < right.size(); ++k){
node = new TreeNode(i);
node->left = left[j];
node->right = right[k];
v.push_back(node);
}
}
}
return v;
}
vector<TreeNode *> generateTrees(int n) {
return generateTreesRec(, n);
}
};
												

【leetcode】 Unique Binary Search Trees II (middle)☆的更多相关文章

  1. 【leetcode】Unique Binary Search Trees II

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

  2. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  3. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  4. 【Leetcode】【Medium】Unique Binary Search Trees II

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

  5. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. 【题解】【BST】【Leetcode】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 【Leetcod】Unique Binary Search Trees II

    给定结点数n,结点值为1,2,...,n,求由这些结点可以构成的所有二叉查找树. Given n, generate all structurally unique BST's (binary sea ...

  8. 【树】Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  9. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. 阿里云9折推荐码:0LGVW2

    阿里云9折推荐码:0LGVW2,第一次购买云服务器或云数据库可享受原价9折优惠.

  2. Form表单中method为get和post的区别

    序,form表单中的方法分为get和post,但你都知道他们之间的区别吗? Form表单中method为get和post的区别: 例子如下,有个Form表单. <form action=&quo ...

  3. 【C语言入门教程】2.7 表达式

    表达式由运算符.常量及变量构成,C语言的表达式基本遵循一般代数规则.有几种运算法则是 C 语言表达式特有的. 2.7.1 表达式中的类型转换 同一表达式中的不同类型常量及变量在运算时需要变量为同一数据 ...

  4. Linux C select函数详解

    select IO复用机制: http://www.cnblogs.com/hjslovewcl/archive/2011/03/16/2314330.html http://blog.csdn.ne ...

  5. MySQL Python教程(1)

    首先对于数据库的基本操作要有一定基础,其次了解Python的基础语法. 建议先阅读MysqL中文教程http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chap ...

  6. linux 学习之路

    很多同学接触Linux不多,对Linux平台的开发更是一无所知. 而现在的趋势越来越表明,作为一个优秀的软件开发人员,或计算机IT行业从业人员, 掌握Linux是一种很重要的谋生资源与手段. 下来我将 ...

  7. word文档的生成、修改、渲染、打印,使用Aspose.Words

    无需MS Word也可执行各种文档处理任务,包括文档的生成.修改.渲染.打印,文档格式转换和邮件合并等文档处理.

  8. mac 下载安装 IntelliJ IDEA Tomcat

    (1)Download IntelliJ IDEA https://www.jetbrains.com/idea/download/ (2)找了个激活码 http://www.oschina.net/ ...

  9. linux kernel 杂谈

    首先介绍一下背景吧,工作三个星期了.复习了一波u-boot,跟了一下事件上报,搞了下平台设备,扣了一个内存检查代码. 想想生活是不是有点无聊.对啊,真的很无聊!!!! 无聊也没有办法啊,所以找点方法去 ...

  10. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...