LeetCode(95) Unique Binary Search Trees II
题目
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.
confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.
OJ’s Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.
Here’s an example:
The above binary tree is serialized as “{1,2,3,#,#,4,#,#,5}”.
分析
给定整数n,求输入元素为[1,n]时,所构成的全部二叉查找树;
我们都知道二叉查找树的特点,左子树节点值小于根节点,右子树节点值大于根节点。
对于输入[1,n],每个值 i 都可以作为根节点,小于i 的元素构成左子树,大于i 的元素构成右子树。
所以,此题的解决办法为二叉树常用递归。
AC代码
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
if (n <= 0)
return vector<TreeNode *>(1 , NULL);
//对值为 [1 , n]的每个元素都可做二叉查找树的根节点
return generateTrees(1, n);
}
//构造根节点[lhs , rhs]的所有二叉查找树
vector<TreeNode *> generateTrees(int lhs, int rhs)
{
if (lhs > rhs)
{
return vector<TreeNode *>(1 , NULL);
}
//存储每个查找树的根节点
vector<TreeNode *> ret;
for (int r = lhs; r <= rhs; r++)
{
//[lhs~r-1]间节点作为左子树,[r+1~rhs]间节点作为右子树
vector<TreeNode *> lefts = generateTrees(lhs, r - 1);
vector<TreeNode *> rights = generateTrees(r + 1, rhs);
//链接符合要求的左右子树
int lsize = lefts.size();
int rsize = rights.size();
for (int i = 0; i < lsize; ++i)
{
for (int j = 0; j < rsize; ++j)
{
//当前节点作为根节点
TreeNode *root = new TreeNode(r);
root->left = lefts[i];
root->right = rights[j];
ret.push_back(root);
}//for
}//for
}//for
return ret;
}
};
LeetCode(95) Unique Binary Search Trees II的更多相关文章
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- flask_之URL
URL篇 在分析路由匹配过程之前,我们先来看看 flask 中,构建这个路由规则的两种方法: 通过 @app.route() decorator 通过 app.add_url_rule,这个方法的签名 ...
- System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.'
下午在调试的时候报错数据库连接就报错我就很纳闷后面用原来的代码写发现还是报错 System.TypeInitializationException: 'The type initializer for ...
- Django之Form组件整理
搬运自:http://www.cnblogs.com/haiyan123/p/7795771.html 一.Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验 ...
- nodejs中的异步回调机制
1.再次clear Timer定时器的作用 setTimeOut绝非是传统意义上的“sleep”功能,它做不到让主线程“熄火”指定时间,它是用来指定:某个回调在固定时间后插入执行栈!(实际执行时间略长 ...
- 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载二(生命周期)
4.1 什么是生命周期 想要真正地理解PhoneGap应用开发的内涵,首先需要理解什么是生命周期.这在字面上其实非常容易理解,一个应用从开始运行被手机加载到应用被退出之间的过程就称之为一个生命周期.为 ...
- SIT&UAT
- LR中订单流程脚本
Action(){ /* 主流程:登录->下订单->支付订单->获取订单列表 定义事物 1)登录 2)下订单 3)支付订单 4)获取订单列表 接口为:application/json ...
- 查看Windows激活信息
使用 Windows + R组合快捷键打开运行命令框 1.运行: slmgr.vbs -dlv 可以查询到Win10的激活信息,包括:激活ID.安装ID.激活截止日期等信息. 2.运行: slmgr. ...
- 如何启动Intel VT-x
如何启动Intel VT-x 5 在64bit win7系统下安装了Vmware10,然后安装64位的UbuntuKylin 14.04,想要打开UbuntuKylin,弹出如下对话框: 请问该如何启 ...
- sql server 处理分母为空
SP 前面加下面设置,会忽略错误结果 直接返回null 不会导致SP 失败 SET ANSI_WARNINGS OFFSET ARITHABORT OFFSET ARITHIGNORE ON