Unique Binary Search Trees

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

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

Unique Binary Search TreesII

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

分析:这个题目是要求BST有多少种排列方式,第一题是只求数量,第二题要求把结构也存储下来。本质都是深搜,第一题显然更简单

因为只要知道当前有多少个数,就可以知道有多少种排列方式了,甚至可以简单的建个存储表,更快一些。而第二题就需要将当前情况

下所有点的排列搞到,返回上一级,上一级再建立更高一层的bst

第一题代码:

class Solution {
public:
int numTrees(int n) {
if(n==) return ;
if(n==) return ;
int wnum = ;
for(int i=;i<=n;i++)
{
wnum += numTrees(i-) * numTrees(n-i);
} return wnum;
}
};

第二题代码:

 /**
* 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 *>builtree(int left,int right)
{
vector<TreeNode *>data;
if(left>right)
{
data.push_back(nullptr);
return data;
} vector<TreeNode *>leftr,rightr;
for(int i=left;i<=right;i++)
{ leftr = builtree(left,i-);
rightr = builtree(i+,right);
for(int t1=;t1<leftr.size();t1++)
for(int t2=;t2<rightr.size();t2++)
{
TreeNode *nod = new TreeNode(i);
nod->left = leftr[t1];
nod->right = rightr[t2];
data.push_back(nod);
}
}
return data; }
vector<TreeNode *> generateTrees(int n) {
vector<TreeNode *> data(,nullptr);
data = builtree(,n);
return data;
}
};

Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II的更多相关文章

  1. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  2. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  3. [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  4. [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  5. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  6. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  7. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  8. pat04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  9. [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

    Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...

  10. 【Leetcode_easy】700. Search in a Binary Search Tree

    problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...

随机推荐

  1. Ubuntu通过使用PyCharm 进行调试 Odoo 8.0 可能出现的问题

    实现步骤,请移步http://shine-it.net/index.php?topic=16603.0 要么 http://www.mindissoftware.com/2014/09/11/Run- ...

  2. Mono 4 和Jexus 5.6

    Mono 4 和Jexus 5.6 概述 在这篇文章中我们将讨论如何在CentOS 7操作系统,安装 jexus. mono 和 配置 jexus,因此它将能够在这种环境中运行一个asp.net mv ...

  3. SettingsProvider该CRUD

    转载请注明出处:http://blog.csdn.net/droyon/article/details/35558697 什么时候delete要么update时间.需要清空缓存并重新加载数据. 1.i ...

  4. Visual Studio-Sequence Diagram

    UML Design Via Visual Studio-Sequence Diagram 本文主要介绍在Visual Studio中设计时序图,内容如下: 何时使用时序图 时序图元素介绍 条件.循环 ...

  5. CSS3之重新定义鼠标右键

    效果图: html: <div id="rightkey"> <ul> <li><img src="images/xmgl.pn ...

  6. Tomcat剖析(四):Tomcat默认连接器(1)

    Tomcat剖析(四):Tomcat默认连接器(1) 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三): ...

  7. 【Unity 3D】学习笔记三十八:角色控制器

    角色控制器 在unity中,已经帮我们实现的上下左右跳等动作,并将他们封装成了角色控制器.角色控制器保存在unity标准资源包中,能够说是很的强大.能够模拟第一或者第三人称视角.不受刚体的限制,很适用 ...

  8. Java初认识--Java语言的书写规范及基本的运算符

    一.Java中名称的规范和书写程序的规范. 1.Java中的名称规范: (1)包名全是小写:xxyyzz: (2)类名接口名:首字母大写:XxxYyy: (3)变量名和函数名:变量名不能是关键字:多单 ...

  9. ASP.NET学习笔记2--自己写代码绑定Gridview

    像以前一样,先写好自己的样式布局, 第二步,在数据库建立一个商品表 代码如下: CREATE TABLE [SHANGPING_INFO] ( [Shangping_Id] INT PRIMARY K ...

  10. c# md5

              还可以加盐,更难以破解 public static string GetMD5(string sDataIn)           {               MD5Crypt ...