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. (15)ServletConfig对象详解

    1,作用 主要是用于加载servlet的初始化参数.在一个web应用可以存在多个ServletConfig对象(一个Servlet对应一个ServletConfig对象) 2,创建时机和对象的获取 创 ...

  2. XMU 1056 瞌睡 vs 听课 【动态规划】

    1056: 瞌睡 vs 听课 Time Limit: 500 MS  Memory Limit: 64 MBSubmit: 19  Solved: 6[Submit][Status][Web Boar ...

  3. 解决Linux环境Oracle显示乱码

    首先查看当前的编码格式 select userenv('language') from dual; 解决方法:   一.临时解决方法   切换到Oracle用户,执行   export NLS_LAN ...

  4. O(n²)、O(n)、O(1)、O(nlogn)

    大体上和 @丁戍 说的差不多. 简单说O(n²)表示当n很大的时候,复杂度约等于Cn²,C是某个常数,简单说就是当n足够大的时候,n的线性增长,复杂度将沿平方增长. O(n)也是差不多的意思,也就是说 ...

  5. POJ3020 Antenna Placement —— 最大匹配 or 最小边覆盖

    题目链接:https://vjudge.net/problem/POJ-3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K ...

  6. casperjs userAgent的一些问题

    casperjs 的options内的userAgent若设置为非正常浏览器的字符串,可能导致form无法正确提交. 表现为,this.click()失效,或evaluate(function(){$ ...

  7. 性能-发挥ORACLE分区表

    ORACLE分区表发挥性能 http://www.cnblogs.com/zwl715/p/3962837.html 1.1 分区表PARTITION table 在ORACLE里如果遇到特别大的表, ...

  8. BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论

    BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论 Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作 ...

  9. Python科学计算工具包

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

  10. python 面向对象八 多继承

    python是支持多继承的,在设计类的继承关系时,通常,主线都是单一继承下来的.但是,如果需要“混入”额外的功能,通过多重继承就可以实现,这种设计通常称之为MixIn. 为了更好地看出继承关系,以Mi ...