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. Tomcat 6 JNDI数据源详解

    数据库连接池这个概念应该都不陌生,在Java中连接池也就是数据库的连接池,它是一种采用连接复用的思想避免多次连接造成资源的浪费机制. 最常见的连接池就是DBCP和C30P了,在tomcat中默认使用的 ...

  2. Memcached原理分析

    Memcached的内存管理方式 Memcached采用了名为Slab Allocation的机制分配,管理内存. Slab Allocation的原理相当简单.将分配的内存分割成各种尺寸的块(chu ...

  3. Ali相关面试题

    接到的电话面试,人比较随和,当时IOS有一段时间没怎么碰了,因为近期一直在用C++,QT做IM.很多回答我都扯到了C++上,所以可能没戏- -! 回想一下,大概有如下几个问题:(都是很常见的问题) 1 ...

  4. 数论只会GCD。。。

    一些关于GCD的代码.... #include <iostream> #include <cstdio> #include <cstring> using name ...

  5. Ubuntu 12 安装 搜狗输入法

    下载地址:http://pinyin.sogou.com/linux/?r=pinyin Ubuntu 12 中,安装搜狗输入法注意事项 http://pinyin.sogou.com/linux/h ...

  6. PHP Socket实现websocket(四)Select函数

    int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); /* ...

  7. android Home键和返回键

    在Android中,当按下Home键,默认情况下stop前台的actiity,即activity设置成onstop,而不是ondestory.如果再次启动该activity不是调用onCreate,而 ...

  8. Maven生命周期和插件机制

    Maven中的一个非常重要的概念是生命周期和插件,这篇文章重点介绍下Maven的生命周期. Maven的生命周期是抽象的,具体的功能是有具体的插件来完成的,Maven有相当多的功能插件,以至于Mave ...

  9. 总结六条对我们学习Linux系统有用的忠告

    接触linux需要的是端正自己的态度,这个玩意可不是一天两天就能拿得下的.学习个基础,能装系统.能装常见服务.能编译.能配置存储空间.能配置系统参数.能简单查看系统负载等基本够用.但这些只保证能做机房 ...

  10. Web服务精讲–搭个 Web 服务器(二)

    导读 曾几何时,你所选择的 Python Web 框架会限制你所可选择的 Web 服务器,反之亦然.如果某个框架及服务器设计用来协同工作的,那么一切正常. 在第一部分中,我提出了一个问题:“如何在你刚 ...