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. Redis+Sentinel安装与配置

    在这里我们搭建的是一个1主3从的redis+3个哨兵集群的环境,由于是在一台物理机上,所有我们用端口区分. 物理机IP:192.168.0.12 主节点master端口:6301 从节点slave1端 ...

  2. 06-【servletconfig、servletContext 】

    ServletConfig.ServletContext 1.ServletConfig获取web.xml中的配置信息:java代码: @Override public void init(Servl ...

  3. QT字符串QString

    字符串转数值 --------------------------------------------------------------------------------------------- ...

  4. Mysql 5.6主从同步配置

    主从同步,本质是利用数据库日志,将主库数据复制一份到从库,本质上是使用了数据复制技术. 本文概要 主库的基本配置 从库的基本配置 完全同步的步骤 注意事项 工作原理 1. 主库的基本配置 做两件事:启 ...

  5. 移动端H5不常见兼容收集

    1.微信分身input不支持上传,无法监听到上传事件 解决方案:判断在微信浏览器端使用微信JSSDK上传

  6. BZOJ 4128: Matrix (矩阵BSGS)

    类比整数的做法就行了 1A爽哉 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int M ...

  7. Oracle查询死锁

    select sample_time,session_id,sql_id,event,sql_plan_hash_value,blocking_session from dba_hist_active ...

  8. cefsharp文档

    原文链接:https://github.com/cefsharp/CefSharp/wiki/CefSharp中文帮助文档#a1_1 CefSharp中文帮助文档 目录 基础知识 1.1 cefsha ...

  9. 导出excel 各 cvs 的方法

    public function orderExcelExport($data,$filename='simple.xls'){ ini_set('max_execution_time', '0'); ...

  10. TTTTTTTTTTTTTTTTTT CodeForces 589A Email Aliases 字符串 map

    A - Email Aliases Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u ...