leetcode_894. All Possible Full Binary Trees
https://leetcode.com/problems/all-possible-full-binary-trees/
给定节点个数,求所有可能二叉树,该二叉树所有节点要么有0个子节点要么有两个子节点。返回所有二叉树的头指针。
一开始一直想的是从根节点开始建树,一直想不出来方法。后来想到可以从子节点开始建树,问题就很好解决了。
class Solution
{
public:
vector<TreeNode*> allPossibleFBT(int N)
{
vector<TreeNode*> ret;
if(N == )
{
TreeNode* rt = new TreeNode();
ret.push_back(rt);
return ret;
}
for(int i=; i<=(N-)/; i+=) //左子树的节点数
{
vector<TreeNode*> left = allPossibleFBT(i); //创建所有可能左子树
vector<TreeNode*> right = allPossibleFBT(N--i); //创建所有可能的右子树
for(int j=;j<left.size();j++) //遍历所有左子树
for(int k=;k<right.size();k++) //遍历所有右子树
{
TreeNode * rt = new TreeNode(); //创建根节点
rt->left = left[j];
rt->right = right[k];
ret.push_back(rt);
if(i != N--i) //如果左右子树节点数不同,交换左右子树也是一种可能
{
TreeNode * rt2 = new TreeNode();
rt2->left = right[k];
rt2->right = left[j];
ret.push_back(rt2);
}
}
}
return ret;
}
};
leetcode_894. All Possible Full Binary Trees的更多相关文章
- 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 ...
- [Swift]LeetCode617. 合并二叉树 | 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 ...
- [Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...
- [Swift]LeetCode894. 所有可能的满二叉树 | 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 ...
- [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
随机推荐
- (1)数据库和MySql初步认识
一,数据的保存: 数据可以通过很多方式进行保存,不用的保存方式对于所保存的数据的影响各有不同. 1,数据保存在内存中:读写速度很快:但是随着程序的关闭数据会丢失,而且内存容量相对小,价格昂贵 2,数据 ...
- 比特币客户端Electrum使用介绍
简介 比特币的客户端很多,为什么选择Electrum. 首先Electrum真的很轻量,安装马上可以用,不用下载几百G的区块链账本.我之前安装bitcoin核心客户端,这是个完整节点.下载账本都要好多 ...
- aliyun 日志服务(Log Service,Log)是针对日志场景的一站式服务
日志服务(Log Service,Log)是针对日志场景的一站式服务,在阿里巴巴集团内部被广泛使用.用户无需开发就能快捷完成日志生命周期中采集.消费.投递以及查询功能. 日志服务当前提供如下功能 日志 ...
- mac系统下配置域名映射关系
1.cd /private/etc/ 先修改权限:sudo chmod 777 hosts vi hosts localhost:etc mhx$ cat hosts ## # Host Databa ...
- Nginx反向代理服务器、负载均衡和正向代理
Nginx("engine x")是一个高性能的 HTTP 和反向代理服务器,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日.官方测试 nginx 能够支撑5万 ...
- 【position】
background-position 定位的position的区别 background-position 相对的位置是(容器的宽度-子元素的宽度) 当子元素的宽度大于容器的宽度 backgroun ...
- ChartCtrl源码剖析之——CChartAxisLabel类
CChartAxisLabel类用来绘制轴标签,上.下.左.右都可以根据实际需要设置对应的轴标签.它处于该控件的区域,如下图所示: CChartAxisLabel类的头文件. #if !defined ...
- 使用merge-using语句初始化数据库
创建三张表Student.Course.Enrollment CREATE TABLE [dbo].[Student] ( [StudentID] INT IDENTITY (1, 1) NOT NU ...
- C#6.0的新特性之内插字符串
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/interpolated-strings C# 6 ...
- HDU 5945 Fxx and game (DP+单调队列)
题意:给定一个 x, k, t,你有两种操作,一种是 x - i (0 <= i <= t),另一种是 x / k (x % k == 0).问你把x变成1需要的最少操作. 析:这肯定是 ...