1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
95_不同的二叉搜索树_II_recursion.cpp 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<TreeNode *> buildTree (int from, int to)
{
if (from > to)
return {nullptr};
if (from == to)
return {new TreeNode(to)};
vector<TreeNode *> res;
for (int rootIndex = from; rootIndex <= to; rootIndex++) {
vector<TreeNode *> leftChildren = buildTree(from, rootIndex-1);
vector<TreeNode *> rightChildren = buildTree(rootIndex+1, to);
// root->left = leftChildren;
// root->right = rightChildren;
for (auto l : leftChildren) {
for (auto r : rightChildren) {
TreeNode *root = buildTree(rootIndex,rootIndex)[0];
root->left = l;
root->right = r;
res.push_back(root);
}
}
// res.push_back(root);
}
return res;
}
vector<TreeNode*> generateTrees(int n) {
if (n == 0)
return {};
/* 后续动态规划用 */
map<int, map<int, TreeNode*>> m;
auto res = buildTree(1, n);
return res;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
[email protected]:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助