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的更多相关文章

  1. hdu3240 Counting Binary Trees

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

  2. Binary Trees

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

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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. 编译Android VNC Server【转】

    本文转载自:http://www.cnblogs.com/fengfeng/p/3289292.html 1,在如下地址checkout源代码,我checkout的版本为0.9.7http://cod ...

  2. Recovery启动流程(1)--- 应用层到开机进入recovery详解

    转载请注明来源:cuixiaolei的技术博客 进入recovery有两种方式,一种是通过组合键进入recovery,另一种是上层应用设置中执行安装/重置/清除缓存等操作进行recovery.这篇文档 ...

  3. 2 socket相关概念

    嘿嘿 这只是学习过程中的笔记积累,百度也是一代吧,大神就勿喷勒..... 1 为什么把网络编程接口叫做套接字 socket字面意思为插座 插孔,让人联想到电话,这种简单的设备给人类太大的方便 2 根据 ...

  4. bzoj 1180: [CROATIAN2009]OTOCI【LCT】

    一道几乎是板子的LCT,但是沉迷数学很久时候突然1A了这道题还是挺开心的 #include<iostream> #include<cstdio> using namespace ...

  5. 牛客网NOIP赛前集训营-提高组(第八场)

    染色 链接:https://ac.nowcoder.com/acm/contest/176/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语言10 ...

  6. P5071 [Ynoi2015]此时此刻的光辉

    传送门 lxl大毒瘤 首先一个数的因子个数就是这个数的每个质因子的次数+1的积,然后考虑把每个数分解质因子,用莫队维护,然后我交上去就0分了 如果是上面那样的话,我们每一次移动指针的时间复杂度是O(这 ...

  7. [App Store Connect帮助]七、在 App Store 上发行(3.2)提交至“App 审核”:查看 App 状态历史记录

    您可以查看您 App 的某一版本的 App 状态历史记录.在历史记录表中的每一行都包含 App 状态.App 状态更改时间,以及更改的发起人.使用此信息追踪“App 审核”流程中的 App. 若想在 ...

  8. Ubuntu 18.04 关闭蓝牙开机启动

    sudo gedit /etc/rc.local 然后,加入下面一行 rfkill block bluetooth

  9. linux 重名名、删除文件操作

    linux下重命名文件或文件夹的命令mv既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a目录移动到/b下,并重命名为c mv /a /b/c 删除文件夹 r ...

  10. Kubernetes集群认证

    1.集群搭建:https://www.kubernetes.org.cn/3808.html 2.集群验证:https://www.kubernetes.org.cn/1861.html