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. 2015年12月02日 GitHub入门学习(四)Git操作

    序,学习使用Git是一项新技能,你将了解到Git与SubVersion的区别. 一.基本操作 git init 初始化仓库,请实际建立一个目录并初始化仓库,.git目录里存储着管理当前目录内容所需的仓 ...

  2. 如何申请https证书、搭建https网站

    如何申请https证书.搭建https网站 随着国内搜索引擎巨头百度启用全站https加密服务,全国掀起了网站https加密浪潮.越来越多的站点希望通过部署https证书来解决“第三方”对用户隐私的嗅 ...

  3. Linux程序编写shell script的格式

    #!/bin/bash #program # 在此处写下此程序的作用 #History: #此处写下写此程序的时间 作者 版本号 PATH=/bin:/sbin:/usr/bin:/usr/sbin: ...

  4. oracle数据库表空间扩容方法

    1. 先查询表空间在物理磁盘上存放的位置,注意使用sysdba的账号登陆. SELECT tablespace_name, file_id, file_name, ), ) total_space F ...

  5. 浮动层-JS兼容IE6

    //引用jquery 包/*orderBycat 与 orderBycatHead 双层浮动*/ $(window).scroll(function () { var top = $(window). ...

  6. java中重载与重写的区别

    (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现. 然后我们再来谈谈 重写(Over ...

  7. tomcat中配置jmx监控

    1.在tomcat的start.bat中添加下面代码, -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote. ...

  8. SpringDataJPA的几个使用记录

    public Page<XMGLFileTemplateDTO> findXMGLFileTemplateByConditions(XMGLFileTemplateDTO xmglFile ...

  9. 通过Unity3d创建二维码(利用zxing2.2)

    http://blog.csdn.net/liulala16/article/details/14521979 2013-11-08 14:53 1965人阅读 评论(3) 收藏 举报 首先 下载ZX ...

  10. python 中的unicode详解

    通过例子来看问题是比较容易懂的. 首先来看,下面这个是我新建的一个txt文件,名字叫做ivan_utf8.txt,然后里面随便编辑了一些东西. 然后来用控制台打开这个文件,同样也是截图: 这里就是简单 ...