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. create database 默认utf-8

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci; 这是sql语句 CREATE TA ...

  2. UI:简单的SQL语句

    一.SQL语句如果要在程序运行过程中操作数据库中的数据,那得先学会使用SQL语句1.什么是SQLSQL(structured query language):结构化查询语言SQL是一种对关系型数据库中 ...

  3. Python科学计算工具包

    import numpy as np # 创建一个Ndarray # 1. 从list对象中创建 # 2. 创建一个特定的数组,全1数组ones,随机数组random.randn,对角矩阵diag # ...

  4. React VSCode插件

    可以安装2个插件,一个是 Atuo Rename Tag 一个 Auto Close Tag 这样就好都了

  5. Gym 100531G Grave(水题)

    题意:给定一个大矩形,再给定在一个小矩形,然后给定一个新矩形的长和高,问你能不能把这个新矩形放到大矩形里,并且不与小矩形相交. 析:直接判定小矩形的上下左右四个方向,能不能即可. 代码如下: #pra ...

  6. 类似查询mysql数据库的查询XML的JS类

    一个快捷操作XML数据库的Javascript接口对象,包含select.count.tables.fields等方法,能够像操作mysql等其它数据库一样操作XML数据库. if(document. ...

  7. 哈理工OJ P2320:OX

    题目链接:OX 题意 :给出一个3X3的黑白棋棋盘,棋盘上有若干黑白子,再给出下一个下的人,问下一个下的人能否赢 分析:考虑到只有39种状态,故用一个数保存目前棋盘的状态,记为value,再枚举空位D ...

  8. bzoj 2208: [Jsoi2010]连通数【tarjan+拓扑+dp】

    我总觉得枚举点bfs也行-- tarjan缩点,记一下每个scc的size,bitset压一下scc里的点,然后按拓扑倒序向上合并到达状态,然后加ans的时候记得乘size #include<i ...

  9. bzoj 4195: [Noi2015]程序自动分析【并查集】

    等于有传递性,所以hash一下把等于用并查集连起来,然后再判断不等于是否合法即可 #include<iostream> #include<cstdio> #include< ...

  10. SpringMVC之HttpMessageConverter

    http://blog.csdn.net/zmx729618/article/details/53034420 HttpMessageConverter接口: T read(Class<? ex ...