【题目】
Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.
For example,
Givenn= 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
confused what"{1,#,2,3}"
means?>
read more on how binary tree is serialized on OJ.
【分析】
参考:[LeetCode]96.Unique Binary Search Trees
【代码】
/*********************************
* 日期:2014-12-27
* 作者:SJF0115
* 题目: 95.Unique Binary Search Trees II
* 来源:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/
* 结果:AC
* 来源:LeetCode
* 总结:
**********************************/
#include <iostream>
#include <vector>
using namespace std;
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) {
if (n <= 0){
return generate(1, 0);
}//if
else{
return generate(1, n);
}
}//
private:
// 返回根节点结合
vector<TreeNode*> generate(int start,int end){
vector<TreeNode*> subTree;
if(start > end){
subTree.push_back(NULL);
return subTree;
}//if
// i作为根节点
for(int i = start;i <= end;i++){
// 以i为根节点的树,其左子树由[start,i-1]构成,其右子树由[i+1,end]构成。
// 返回的是不同二叉查找树的根节点,几种二叉查找树就返回几个根节点
vector<TreeNode*> leftSubTree = generate(start,i-1);
vector<TreeNode*> rightSubTree = generate(i+1,end);
// 左子树右子树跟根节点连接
// 以i为根的树的个数,等于左子树的个数乘以右子树的个数
for(int j = 0;j < leftSubTree.size();j++){
for(int k = 0;k < rightSubTree.size();k++){
TreeNode* node = new TreeNode(i);
node->left = leftSubTree[j];
node->right = rightSubTree[k];
subTree.push_back(node);
}//for
}//for
}//for
return subTree;
}//
};
// 先序遍历
void PreOrder(TreeNode* root){
if(root == NULL){
return;
}//if
cout<<root->val<<" ";
PreOrder(root->left);
PreOrder(root->right);
}
int main() {
Solution solution;
vector<TreeNode*> vec = solution.generateTrees(3);
for(int i = 0;i < vec.size();i++){
TreeNode* root = vec[i];
PreOrder(root);
cout<<endl;
}
}
分享到:
相关推荐
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
Leetcode 题解.pdf
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 后端.zip
Leetcode101.zip
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
刷leetcode总结.md
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
Leetcode 92. 反转链表 II问题描述算法解法1: 递归图示解法1:实现def reverseBetween(self, head: ListNode
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
My Solutions to Leetcode problems. All solutions support C
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
LeetCode674. 最长连续递增序列674. 最长连续递增序列解题思路:记录每次递增序列的长度,max存储最大长度// 递增序列更新最大长度} else