full binary tree is a binary tree where each node has exactly 0 or 2 children.

Return a list of all possible full binary trees with N nodes.  Each element of the answer is the root node of one possible tree.

Each node of each tree in the answer must have node.val = 0.

You may return the final list of trees in any order.

Example 1:

Input: 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
Explanation:

Runtime: 104 ms, faster than 53.23% of C++ online submissions for All Possible Full Binary Trees.

用map来优化。

class Solution {
private:
unordered_map<int,vector<TreeNode*>> mp;
public:
vector<TreeNode*> allPossibleFBT(int N) {
vector<TreeNode*> ret, leftvec, rightvec;
if(N & == || N <= ) return ret;
return helper(N);
}
vector<TreeNode*> helper(int N){
vector<TreeNode*> leftvec, rightvec, ret;
if(mp.count(N)) return mp[N];
if(N == ) return {new TreeNode()};
for(int i=; i < N; i+=){
leftvec = helper(i);
rightvec = helper(N - i -);
//cout << leftvec.size() << rightvec.size() << endl;
for(int l = ; l<leftvec.size();l++){
for(int r = ; r<rightvec.size();r++){
TreeNode* tmp = new TreeNode();
tmp->left = leftvec[l];
tmp->right = rightvec[r];
ret.push_back(tmp);
}
}
}
//cout << ret.size() << endl;
mp[N] = ret;
return ret;
}
};

LC 894. All Possible Full Binary Trees的更多相关文章

  1. [LeetCode] 894. All Possible Full Binary Trees 所有可能的满二叉树

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  2. 【LeetCode】894. All Possible Full Binary Trees 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. Leetcode 894. All Possible Full Binary Trees

    递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...

  4. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

  5. hdu3240 Counting Binary Trees

    Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  6. Binary Trees

    1. Definiation What is Binary Trees? Collection of node (n>=0) and in which no node can have more ...

  7. [leetcode-617-Merge Two Binary Trees]

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  8. Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  9. [LeetCode] Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

随机推荐

  1. Shell脚本变量与判断

    变量 环境变量 局部变量 declare 定义变量类型 本地变量 local 在函数中使用 变量类型: 数值型: 1.整形 2.浮点型 3.布尔型 0 1 (真 假) (true false) 字符串 ...

  2. 实现单点登录功能的思路以及kafka同步数据

    单点登录以及用户数据同步思路与方案 当公司业务分布于多个子系统时, 同一用户在A系统注册,即可在其他所有关联系统使用, 并支持登录A系统后,自动在其他系统登录,退出同理. 在A平台修改通用的用户数据( ...

  3. Bootstrap常用的自带插件

    Bootstrap自带的那些常用插件. 模态框 模态框的HTML代码放置的位置 务必将模态框的HTML代码放在文档的最高层级内(也就是说,尽量作为 body 标签的直接子元素),以避免其他组件影响模态 ...

  4. deep_learning_Softmax()

    该节课中提到了一种叫作softmax的函数,因为之前对这个概念不了解,所以本篇就这个函数进行整理,如下: 维基给出的解释:softmax函数,也称指数归一化函数,它是一种logistic函数的归一化形 ...

  5. PAT 1003.我要通过1

    答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: ...

  6. zencart更改css按钮的宽度css buttons

    includes\functions\html_output.php 大概323行的zenCssButton函数 function zenCssButton($image = '', $text, $ ...

  7. SPOJ 1825 经过不超过K个黑点的树上最长路径 点分治

    每一次枚举到重心 按子树中的黑点数SORT一下 启发式合并 #include<cstdio> #include<cstring> #include<algorithm&g ...

  8. 纯c中声明变量

    count 声明位置错了,纯c中声明变量全部放在main函数里,最前面的位置,要么vc6.0会报错: 修改 #include <stdio.h> int main(void) { int ...

  9. maven将本地jar包引入本地maven仓库命令

    一.maven安装命令.cmd窗口,idea中均可 mvn install:install-file -Dfile=F:\coding2pay\pay\lib/wxpay-sdk-.jar -Dgro ...

  10. fastclick无需对IOS11及以上做处理

    // 解决click点击300毫秒延时问题 (IOS11及以上无需处理) import FastClick from 'fastclick' const device = navigator.user ...