https://leetcode.com/problems/all-possible-full-binary-trees/

给定节点个数,求所有可能二叉树,该二叉树所有节点要么有0个子节点要么有两个子节点。返回所有二叉树的头指针。

一开始一直想的是从根节点开始建树,一直想不出来方法。后来想到可以从子节点开始建树,问题就很好解决了。

  1. class Solution
  2. {
  3. public:
  4. vector<TreeNode*> allPossibleFBT(int N)
  5. {
  6. vector<TreeNode*> ret;
  7. if(N == )
  8. {
  9. TreeNode* rt = new TreeNode();
  10. ret.push_back(rt);
  11. return ret;
  12. }
  13. for(int i=; i<=(N-)/; i+=) //左子树的节点数
  14. {
  15. vector<TreeNode*> left = allPossibleFBT(i);    //创建所有可能左子树
  16. vector<TreeNode*> right = allPossibleFBT(N--i); //创建所有可能的右子树
  17. for(int j=;j<left.size();j++)     //遍历所有左子树
  18. for(int k=;k<right.size();k++) //遍历所有右子树
  19. {
  20. TreeNode * rt = new TreeNode(); //创建根节点
  21. rt->left = left[j];
  22. rt->right = right[k];
  23. ret.push_back(rt);
  24. if(i != N--i)    //如果左右子树节点数不同,交换左右子树也是一种可能
  25. {
  26. TreeNode * rt2 = new TreeNode();
  27. rt2->left = right[k];
  28. rt2->right = left[j];
  29. ret.push_back(rt2);
  30. }
  31. }
  32. }
  33. return ret;
  34. }
  35. };

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. 每天进步一点点—mysql-mysqldump

    一.        简单介绍 mysqldump是client用来备份数据库或者在不通数据库之间进行数据迁移的工具,备份内容包括创建表或者装载表的SQL语句 二.       命令格式   备份单个数 ...

  2. [10.26_P2] 最短路 (单源最短路应用)

    单源最短路问题拓展 Description 给你一张图,图上有 n 个点,m 条边,要你找到两个点,使其最短路恰好包含给定的 k 个点.输出这条最短路的长度,输入保证有解. 输入格式 第一行两个数 n ...

  3. Iterator 使用

    Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactor ...

  4. POJ - 3352 Road Construction(边双连通分支)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板)  与这道题一模一样.代码就改了下范围,其他都没动 ...

  5. codeforces 686A A. Free Ice Cream(水题)

    题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...

  6. Educational Codeforces Round 22 补题 CF 813 A-F

    A The Contest 直接粗暴贪心 略过 #include<bits/stdc++.h> using namespace std; int main() {//freopen(&qu ...

  7. bzoj4031

    4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 823  Solved: 407[Submit][Statu ...

  8. 创建swagger的springboot-stater,并在spring cloud zuul网关中引入

    Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...

  9. Luogu P3619 魔法 【贪心/微扰证明】

    题目描述 cjwssb知道是误会之后,跟你道了歉.你为了逗笑他,准备和他一起开始魔法.不过你的时间不多了,但是更惨的是你还需要完成n个魔法任务.假设你当前的时间为T,每个任务需要有一定的限制ti表示只 ...

  10. eurekaclient向eurekaserver注册使用真实ip设置

    有时候eureka.instance.prefer-ip-address=true不管用,解决办法如下.