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

分析:

递归即可。枚举所有的 root 节点,root 的左子树和右子树就是两个子问题,递归求解。

但是有一点要注意,容易出错。

如果子树的节点个数为零,这时候返回的应该是一个包含 NULL 的 vector;

/**
* 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<TreeNode*> res;
if(n == 0){
res.push_back(NULL);
return res;
}
return generateTrees(1, n);
}
private:
vector<TreeNode *> generateTrees(int left, int right)
{
TreeNode *node(NULL);
vector<TreeNode*> trees, left_tree, right_tree;
if(left > right)
{
trees.push_back(NULL);
return trees;
}
for(int i=left; i<=right; ++i)
{
left_tree = generateTrees(left, i-1);
right_tree = generateTrees(i+1, right);
for(auto j : left_tree)
for(auto k : right_tree)
{
node = new TreeNode(i);
node->left = j;
node->right = k;
trees.push_back(node);
}
}
return trees;
}
};

LeetCode----Unique Binary Search Trees 2的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  4. LeetCode: Unique Binary Search Trees II 解题报告

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

  5. LeetCode - Unique Binary Search Trees II

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

  6. Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II

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

  7. [leetcode]Unique Binary Search Trees @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...

  8. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  9. Leetcode Unique Binary Search Trees

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

  10. LeetCode: Unique Binary Search Trees [095]

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

随机推荐

  1. Excel中的隐藏函数

    excel有一些隐藏函数,这些函数在帮助文件中找不到介绍,常用的有以下三个: 1.DATEDIF() 功能:计算两个日期的差值 语法:DATEDIF(start_date,end_date,unit) ...

  2. QT-- MainWindow外的cpp文件调用ui

    这几天在学习QT,想写一个类似VIM的小软件,刚开始不注重代码结构,全部实现都写在MainWindow文件中,导致MianWindow文件十分的长而且很难去阅读,就想着把函数按照功能分到不同的cpp文 ...

  3. hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. JSON Viewer

    http://jsonviewer.codeplex.com/ jsoneditor https://github.com/josdejong/jsoneditor

  5. hdu5876 Sparse Graph(补图最短路 bfs)

    题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...

  6. QA16复制_新增查询条件,修改批量使用决策

    需求: 增加评估代码,检验类型条件.(检验批中部分检验项目未录结果的检验批显示    注:标准的程序,不支持空结果的查询和使用决策) 1.复制 RQEVAI10 程序 2.因为这是用的QM模块的逻辑数 ...

  7. CSS3 Media Queries

    Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel= ...

  8. java并发带返回结果的批量任务执行

    转载:http://www.it165.net/pro/html/201405/14551.html 一般情况下,我们使用Runnable作为基本的任务表示形式,但是Runnable是一种有很大局限的 ...

  9. input固定定位后,当input框获取到焦点时,会离开手机软键盘的解决方法

    前些天做页面时候遇到这样一个问题,将input框用position:fixed固定定位在最底部的时候,当Input获取焦点的时候,input框离开了手机软键盘,而不是吸附在软键盘上,效果如下图: 找了 ...

  10. spring的依赖注入DI(IOC)

    1.手动注入 (1)set注入 public class UserService { private UserDao userDao; public void setUserDao(UserDao d ...