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. 在Orchard CMS Theme 用代码定义布局Widgets

    因为公司业务需要,经过本人几个月尝试,使用Orchard CMS 开发一个简单的企业门户(地址是http://www.ubof.cn).在刚开始接触Orchard CMS,对于个性化的网页布局不知道怎 ...

  2. XMU 1607 nc与点对距离 【线段树】

    1607: nc与点对距离 Time Limit: 5000 MS  Memory Limit: 512 MBSubmit: 60  Solved: 8[Submit][Status][Web Boa ...

  3. UI:数据的解析XML与JSON

    XML  和  JSON 语言  本篇博客来自互联网参考 XML 和 JSON 的互相转化 有属性的转化为对象,无属性的转化为字符串 节点的顺序性不可逆,XML有顺序,JSON 无顺序 XML 和 J ...

  4. asp.net mvc4 新特性

    摘自:ASP.MVC Web编程 几种模板的解释

  5. HDU 5912 Fraction (模拟)

    题意:给定一个分式,让你化简. 析:从分母开始模拟分数的算法,最后约分. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...

  6. bzoj 2811: [Apio2012]Guard【线段树+贪心】

    关于没有忍者的区间用线段树判就好啦 然后把剩下的区间改一改:l/r数组表示最左/最右没被删的点,然后删掉修改后的左边大于右边的:l升r降排个序,把包含完整区间的区间删掉: 然后设f/g数组表示i前/后 ...

  7. [App Store Connect帮助]八、维护您的 App(4.1)监控顾客评论:评分与评论概述

    App Store 上的评分与评论 顾客可以按照 1 星至 5 星的级别对您的 App 进行评分.顾客还可为您的 iOS 和 macOS App 撰写评论,但无法为 Apple TVOS App 撰写 ...

  8. 水题不AC,自挂二叉树——Chemist

    学长让我们刷USACO的水题果然是有道理的,做了四道挂了两道...细节处理一定要小心!大概都是NOIP Day1 T1的难度,但是一定要考虑全面否则还是凉凉啊. 一.USACO1.1贪婪的送礼者 题目 ...

  9. setsockopt()函数功能介绍

    功能描述: 获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时, 选项位于的层和选项的名称必须给出.为了操作套接字层的选项,应该 将层的 ...

  10. Android偏好设置(7)自定义Preference,和PreferenceDialog

    Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...