Unique Binary Search Trees II - LeetCode
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
思路:通过递归实现。我们设置一个生成节点编号st到ed的子树的递归函数,对于给定的st到ed,我们枚举中间的每一个数当作root节点,假设数为i,则继续调用参数为st到i-1以及i+1到ed的该函数生成所有左孩子和右孩子子树。
/**
* Definition for a binary tree node.
* 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) {
return gen_sub(, n);
}
vector<TreeNode*> gen_sub(int st, int ed)
{
vector<TreeNode*> res;
if (st > ed)
res.push_back(NULL);
for (int i = st; i <= ed; i++)
{
vector<TreeNode*> lnodes = gen_sub(st, i - );
vector<TreeNode*> rnodes = gen_sub(i + , ed);
for (TreeNode *ln : lnodes)
for (TreeNode *rn : rnodes)
{
TreeNode *root = new TreeNode(i);
root->left = ln;
root->right = rn;
res.push_back(root);
}
}
return res;
}
};
Unique Binary Search Trees II - LeetCode的更多相关文章
- Unique Binary Search Trees II leetcode java
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- 【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
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 ...
- 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]* ...
- 41. 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 ...
随机推荐
- TCP/IP网络编程之套接字的多种可选项
套接字可选项进而I/O缓冲大小 我们进行套接字编程时往往只关注数据通信,而忽略了套接字具有的不同特性.但是,理解这些特性并根据实际需要进行更改也十分重要.之前我们写的程序在创建好套接字后都是未经特别操 ...
- Spring Boot 开发系列一 开发环境的一些九九
从今天开始写这个Spring Boot 开发系列,我是第二周学习JAVA的,公司号称springboot把JAVA的开发提升到填空的能力,本人是NET转JAVA的,想看看这个填空的东西到底有多强.废话 ...
- leetcode 【 Sort Colors 】python 实现
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- Python2 HTMLTestRunner自动化测试报告美化
python2 的测试报告美化,需要的同学直接用 #coding=utf-8 """ A TestRunner for use with the Python unit ...
- python 学习分享-实战篇选课系统
# 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...
- mojoportal在IE10中点击ImageButton出错的处理方法
在ie10中,如果点击了mojoportal中的imagebutton,会出现错误,在ie10之前的浏览器,及ie10的兼容模式中及谷歌浏览器中都不会出现. 日志中 错误信息如下: 2013-09-2 ...
- jquery版右下角弹窗效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Git详解之二 Git基础 转
http://www.open-open.com/lib/view/open1328069733264.html Git 基础 读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 ...
- C#中找不到MouseWheel事件的解决办法
在.....Designer.cs中加入 this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this. ...
- CSA Round #84 The Sprawl
题目 Analysis 曼哈顿距离($L1$ metric)最小生成树. Implementation 下面的代码参考了 gispzjz 在比赛中的提交. #include <bits/stdc ...