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. Jenkins + robot framework + git持续集成

    安装略过... 一.Jenkins安装插件 进入系统管理—插件管理—可选插件下安装以下插件Git Client Plugin.GIT plugin.GitHub API Plugin.GitHub p ...

  2. Studio-Class Diagram

    UML Design Via Visual Studio-Class Diagram 用过几个建模设计工具,小的有staruml,大的有rational rose,EA.最后发现还是Visual St ...

  3. maven_修改setting ,改为自己私服或者OSC开源中国 [为解决sqlite-jdbc 在中央仓库找不到]

    因为项目要使用到sqlite ,虽然有现成的jar,但是考虑的项目的易用统一管理,决定还是用maven 结果纠结了半天 sqlite-jdbc 在maven默认的仓库根本找不着,于是乎修改 setti ...

  4. [ Swift框架 ] # SwiftyJSON

    Swift写的关于转化JSON数据 https://github.com/SwiftyJSON/SwiftyJSON

  5. windows7股票的,win8残疾人,安装Han澳大利亚sinoxn个时间,sinox它支持大多数windows软体

    腾讯科技: 正如先前所宣布,微软,10一个月31迄今,Windows 7家庭基础版.家庭高级版和旗舰版盒装版本将不再销售.而微软是不会再OEM制造商授予许可数量的三个版本. windows7股票的.由 ...

  6. Oracle分区表转换

    Oracle普通表—>分区表转换(9亿数据量) 背景介绍: 环境:Linux 5.5 + Oracle 10.2.0.4 某普通表T,由于前期设计不当没有分区,如今几年来的数据量已达9亿+, 空 ...

  7. jQuery整理您的笔记----jQuery开始

    Jquery它是一种高速.简明的JavaScript相框,jQuery设计目标:Write Less,Do More(写更少的代码,做很多其他的事情). 一.Jquery框架优势: 1.轻量级 jQu ...

  8. MEF框架在Silverlight中应用(15)

    原文:MEF框架在Silverlight中应用(15) MEF框架在Silverlight中应用 代码下载 MEF框架是微软提供的一个插件框架.应用概括为一句话:输入,输出,组合.下面是具体在Silv ...

  9. RethinkDB创始人教你如何打造一个伟大的互联网产品

    关于作者 我叫Slava Akhmechet,本人是 RethinkDB 的创始人之一,RethinkDB是开源,分布式数据库,旨在帮助开发人员与运营商在打造实时应用时处理无结构数据 如何打造一个伟大 ...

  10. Java_POI之MS-Excel2003(扩展名.xls)升级至MS-Excel2007及以上版本(扩展名.xlsx)技术过程概要

    Java_POI之MS-Excel2003(扩展名.xls)升级至MS-Excel2007及以上版本(扩展名.xlsx)技术过程概要 作者:Eric.Zhang(花名:穿越者7号) 日期:2015年1 ...