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. .NET Reflector 8.2支持VS2013高亮显示和代码地图视图

    Red Gate Software公司最近发布的.NET Reflector 8.2支持Visual Studio 2013,其Reflector 桌面程序能够转换十六进制/十进制值.桌面程序还支持局 ...

  2. 【Linux】rpm -qa 和 rpm -q

    查询一个包是否被安装 # rpm -q < rpm package name>列出所有被安装的rpm package # rpm -qae.g. rpm -qa|grep "pc ...

  3. SQL SERVER数据库索引、外键查找

    1.索引查找 select a.name as tabname ,h.name as idname,h.type_descfrom sys.objects as a right join sys.in ...

  4. php 正则匹配中文

    在javascript中,要判断字符串是中文是很简单的.比如:var str = "php编程";if (/^[\u4e00-\u9fa5]+$/.test(str)) {aler ...

  5. uva---(11549)CALCULATOR CONUNDRUM

    Problem C CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She ...

  6. 【待整理】PS切图基础教程

    http://www.w3cfuns.com/article-442-1-1.html http://www.w3cfuns.com/article-443-1-1.html 其他专题研究: floa ...

  7. 转:怎样在VMware ESXi上 克隆虚拟机

    Cloning virtual machines on VMware ESXi 翻译自http://www.dedoimedo.com/computers/vmware-esxi-clone-mach ...

  8. long long 与 _int64

    本文讨论的是五种常用的C/C++编译器对64位整型的支持,这五种编译器分别是gcc(mingw32),g++(mingw32),gcc(linux i386),g++(linux i386),Micr ...

  9. charCodeAt 和 fromCharCode

    1.charCodeAt() 定义和用法 charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. 方法 charCodeAt() 与 ...

  10. 关于职位的解释---转CSDN的文章

    摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不胜! 通用法则 法则1:忍耐是 ...