当前位置: 首页 > news >正文

leetcode 894. All Possible Full Binary Trees 所有可能的真二叉树-耗时100

Problem: 894. All Possible Full Binary Trees 所有可能的真二叉树

耗时100%

1和3的答案可以手写出来,5是1+(1+3)或者1+(3+1),7是1+(1+5)或者1+(5+1)或者1+(3+3),第一个1是根节点,括号内分别是左节点和右节点,然后(1+5)其中1一种情况5两种情况,所以7共2+2+1=5种情况,9可以写成1+(1+7)、1+(7+1)、1+(2+5)等

所以可以使用记忆化搜索,依次组合起来就可以得到答案,但是需要复制一遍二叉树

Code

class Solution { public: vector<TreeNode*> ret; unordered_map<int, vector<TreeNode*>> ump; TreeNode* copy(TreeNode* root, TreeNode* rt) { if(root==nullptr) return nullptr; if(rt==nullptr) { rt = new TreeNode(0); } rt->left = copy(root->left, rt->left); rt->right = copy(root->right, rt->right); return rt; } // TreeNode* rootroot; // void dfs(TreeNode* root, int n) { // if(n == 0) { // TreeNode* rt = nullptr; // rt = copy(rootroot, rt); // ret.push_back(rt); // delete root->left; // delete root->right; // root->left = nullptr; // root->right = nullptr; // return; // } // root->left = new TreeNode(0); // root->right = new TreeNode(0); // dfs(root->left, n - 2); // dfs(root->right, n - 2); // // delete root->left; // // root->left = nullptr; // // delete root->right; // // root->right = nullptr; // // dfs(root->right, n - 2); // } vector<TreeNode*> allPossibleFBT(int n) { if((n&1)==0) return {}; TreeNode* root; root = new TreeNode(0); if(n==1) { return {root}; }; TreeNode* rt = nullptr; rt = copy(root, rt); ump[1] = {rt}; root->left = new TreeNode(0); root->right = new TreeNode(0); rt = nullptr; rt = copy(root, rt); ump[3] = {rt}; if(n==3) { return ump[3]; } for(int k = 5; k <= n; k += 2) { for(int i = 1; i <= (k-1)/2; i+=2) { for(TreeNode* l : ump[i]) { for(TreeNode* r : ump[k - i - 1]) { if(2*i!=k-1) { root = new TreeNode(0); root->left = l; root->right = r; // rt = nullptr; // rt = copy(root, rt); ump[k].push_back(root); root = new TreeNode(0); root->left = r; root->right = l; // rt = nullptr; // rt = copy(root, rt); ump[k].push_back(root); } else { root = new TreeNode(0); root->left = l; root->right = r; // rt = nullptr; // rt = copy(root, rt); ump[k].push_back(root); } } } } } // dfs(root, n - 1); return ump[n]; } };
http://www.jsqmd.com/news/338060/

相关文章:

  • 我是如何用寒假7天写完初稿的
  • 科研新手如何读文献?从“乱读”到“会读”
  • 小程序定制开发公司哪家好?2026年主流服务商盘点(电商小程序、投票小程序、快速开发小程序公司推荐) - 品牌2025
  • 2026年浙江不锈钢管及加工服务厂家推荐:异型不锈钢管、201不锈钢管、304不锈钢管、316L不锈钢管、不锈钢管弯圆加工、浙江佳麒不锈钢、以专业品质适配多元场景需求 - 海棠依旧大
  • 【毕业设计】基于Python计算机视觉答题卡的设计与实现
  • 博士日常:其实再大的困难也就几个小时
  • 2026白转黑加盟项目解析:创业选择需关注哪些核心要素 - 品牌排行榜
  • 绵阳市英语雅思培训机构推荐:2026权威测评出国雅思辅导机构口碑榜单 - 老周说教育
  • 【毕业设计】python基于模板的药品名称识别系统
  • 2026年杉德斯玛特服务卡回收平台哪家好(多方面评测) - 淘淘收小程序
  • 硕士/博士研究生避坑指南
  • 参考文献崩了?用户挚爱的AI论文平台 —— 千笔AI
  • 导师严选10个降AI率网站 千笔帮你轻松降AIGC
  • Oracle AI Database JavaScript 开发人员指南学习手册-整理
  • 11,27(补)
  • 【题解】CF1997E Level Up
  • Cursor 博客园 本地知识库 接入流程规范
  • 绵阳市英语雅思培训机构推荐、2026权威测评出国雅思辅导机构口碑榜单 - 老周说教育
  • 2026年惠州到合肥整车运输公司推荐:到贵阳整车运输 /到长春整车运输 /到兰州整车运输/到拉萨零担运输 /到西宁零担运输精选服务商 - 品牌推荐官
  • PaperRed从入门到精通 AI 写论文软件使用全攻略
  • 2026年去离子水厂家最新推荐:蒸馏水价格、工业软水、超纯水、工业超纯水批发、去离子水选择指南 - 优质品牌商家
  • 2026年特种防护服厂家权威推荐:河北冀龙凤,防酸/阻燃防静电/防辐射/防酸碱防静电防护服源头精选 - 品牌推荐官
  • 从零构建Bash工具库(极简优化版)
  • 中银通支付卡回收方法合集,拆解三种主流操作 - 京回收小程序
  • 【题解】CF1144G Two Merged Sequences
  • 香港申研卷出新高度?留学机构战力与文书红黑榜 - 博客湾
  • Q Cache Visual Attention is Valuable in Less than Half of Decode Layers for Multimodal Large Languag
  • 谁在掌控AI芯片的命脉?全球半导体新金字塔格局解析
  • 2026养发脱发加盟市场新机遇,创业者如何把握行业风口 - 品牌排行榜
  • 上海留学机构大PK:沪上学子的“申请加速器” - 博客湾