LC 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 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的更多相关文章
- [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 ...
- 【LeetCode】894. All Possible Full Binary Trees 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode 894. All Possible Full Binary Trees
递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...
- 17. Merge Two Binary Trees 融合二叉树
[抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...
- hdu3240 Counting Binary Trees
Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Binary Trees
1. Definiation What is Binary Trees? Collection of node (n>=0) and in which no node can have more ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- Delphi 通过ADO连接数据库
- 如何设置树莓派 -Zero 自启动连接WIFI
1. 首先我们需要一台可以读取树莓派跟文件系统的Linux虚拟机(比如Ubuntu) 将树莓派SD卡系统插入电脑,识别并打开rootfs文件夹,切换到`rootfs/etc/wpa_supplican ...
- 网络基础篇之NAT(原理)
一.NAT的产生 由于网络的飞速发展和网络应用的极速增多,致使IPv4可用地址空间逐渐枯竭.尽管IPv6可以在根本上解决地址枯竭问题,但IPv4发展到IPv6还需要一个过渡,而这便产生了NAT. 二. ...
- 1.RPC原理学习
1.什么是RPC:远程过程调用协议 RPC(Remote Procedure Call Protocol)— 远程过程调用协议,是一种通过网络从远程计算机程序上请求服务,而不需要 了解底层网络技术的协 ...
- MyBatis中<![CDATA[ ]]>的使用
原文地址:https://www.cnblogs.com/catgatp/p/6403382.html <![CDATA[]]>和转义字符 被<![CDATA[]]>这个标记所 ...
- PAM安全认证模块
- 今日理解之js
JavaScript 是前端的一门编程语言(也是有逻辑) node.js 支持前端js代码 跑在后端服务器上 Js跟Java什么关系? Js跟Java半毛钱关系都没有!!! 原因是当初Java特别火 ...
- python在pycharm中导包一直出错的问题
之前的net在code的子目录中,怎么调试都无法解决.最后用一个简单粗暴的方式,将net直接拿到项目的根目录中.如此即可
- 留言条.html .js来完成
let data = [{ username: "张桑0", time: "2017-09-07 12:00:00", text: "留言文本内容.& ...
- vue项目,百度地图api高亮选取区域,高亮某个地区,行政区域等
效果如下: var blist = [] ,maxZoom: });// 创建地图实例 var point = new window.BMap.Point(89.48,31.57); map.cent ...