2 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
This is the second time I solve this problem.
Everytime when we encounter a BST problem without quite clear thought, we can resort to Divide & Conquer.
Use recursion to build the left and right subtree, then combine them then return.
In this problem, the left and right subtree can be multiple.
FIRST TRY ERROR: Forget to clear the vector of left of right subtree while apply different root.
Code:
/**
* 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;
} res = gen(1, n);
return res;
} vector<TreeNode *> gen(int start, int end)
{
vector<TreeNode*> res;
if(start == end)
{
TreeNode* tmp = new TreeNode(start);
res.push_back(tmp);
return res;
} vector<TreeNode*> leftsub, rightsub;
for(int i = start; i <= end; i++)
{
leftsub.clear(); // First try error
rightsub.clear(); // First try error if(i == start) leftsub.push_back(NULL);
else leftsub = gen(start, i-1); if(i == end) rightsub.push_back(NULL);
else rightsub = gen(i+1, end); for(int m = 0; m < leftsub.size(); m++)
{
for(int n = 0; n < rightsub.size(); n++)
{
TreeNode* root = new TreeNode(i); // divide & conquer
root->left = leftsub[m];
root->right = rightsub[n];
res.push_back(root);
}
}
} return res;
}
};
2 Unique Binary Search Trees II_Leetcode的更多相关文章
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【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
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 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 ...
- 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
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
随机推荐
- C# Struct结构体里数组长度的指定
typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...
- 基于linux(centos)的svn环境搭建
1. 安装svn yum intall subversion 2. 查看安装位置 rpm -ql subversion 3. 检验svn是否安装成功,查看帮助 svn --help , 看到下图表示成 ...
- 【UWP】解析GB2312、GBK编码网页乱码问题
在WebHttpRequest请求网页后,获取到的中文是乱码,类似这样: <title>˹ŵ��Ϸ���������� - ��̳������ - ˹ŵ��Ϸ����</title ...
- 基于Lease分布式系统重试服务选举
/** * Copyright (c) 2015, www.cubbery.com. All rights reserved. */ package com.cubbery.event.retry; ...
- .Net Framework认知
在托管代码的世界里,应用程序首先被加载到应用程序域(AppDomain)中,然后将应用程序域加载到进程中,一个进程可以包含多个应用程序域,也就是说一个进程可以包含多个应用程序,毕竟应用程序域之间的切换 ...
- 页面解耦—— 统跳协议和Rewrite引擎
原文: http://pingguohe.net/2015/11/24/Navigator-and-Rewrite.html 解耦神器 —— 统跳协议和Rewrite引擎 Nov 24, 2015 • ...
- vs写python扩展资料收集
总结: 1.创建dll工程: 2.增加包含头文件路径 :将python路径下的include加入到包含头文件路径:在工程属性页 C/C++/附加包含目新增<Python>\include目 ...
- LINQ 函数的实战演练测试
1.首先定义一个图书类.专门存放图书的属性信息. 代码如下: //Book.cs using System; namespace LinqTest { public class Book { pu ...
- 共享MFC dULL
>------ 已启动生成: 项目: OSGtest, 配置: Debug Win32 ------1>正在编译...1>AddScene.cpp1>main.cpp1> ...
- xml读取节点
<?xml version="1.0" encoding="utf-8"?> <tplcd type=" product=" ...