LeetCode:95. 不同的二叉搜索树 II

class Solution {

public:

vector<TreeNode*> generateTrees(int n) {

if(n==0){

return vector<TreeNode*>{};

}

return build(1,n);

}

vector<TreeNode*>build(int lo,int hi){

vector<TreeNode*>res;

if(lo>hi){

res.emplace_back(nullptr);

return res;

}

for(int i=lo;i<=hi;i++){

vector<TreeNode*> leftTree=build(lo,i-1);

vector<TreeNode*> rightTree=build(i+1,hi);

for(auto left:leftTree){ //自动匹配类型

for(auto right:rightTree){

TreeNode* root=new TreeNode(i);

root->left=left;

root->right=right;

res.emplace_back(root);

}

}

}

return res;

}

};

相关推荐
西飘客5 小时前
vs 怎么根据dll 生成lib文件
c++·qt
程序员老陆6 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
信奥卷王6 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵6 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG8 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
码完就睡8 小时前
数据结构——红黑树
数据结构
码爸8 小时前
数据结构详细介绍
数据结构
Navigator_Z8 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛9 小时前
cpp学习笔记--常量指针
java·开发语言·算法