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

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

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

class Solution
{
public:vector<TreeNode*> allPossibleFBT(int N){vector<TreeNode*> ret;if(N == 1){TreeNode* rt = new TreeNode(0);ret.push_back(rt);return ret;}for(int i=1; i<=(N-1)/2; i+=2)  //左子树的节点数{vector<TreeNode*> left = allPossibleFBT(i);      //创建所有可能左子树vector<TreeNode*> right = allPossibleFBT(N-1-i);  //创建所有可能的右子树for(int j=0;j<left.size();j++)      //遍历所有左子树for(int k=0;k<right.size();k++)    //遍历所有右子树{TreeNode * rt = new TreeNode(0);   //创建根节点rt->left = left[j];rt->right = right[k];ret.push_back(rt);if(i != N-1-i)      //如果左右子树节点数不同,交换左右子树也是一种可能{TreeNode * rt2 = new TreeNode(0);rt2->left = right[k];rt2->right = left[j];ret.push_back(rt2);}}}return ret;}
};

转载于:https://www.cnblogs.com/jasonlixuetao/p/10582693.html

leetcode_894. All Possible Full Binary Trees相关推荐

  1. LeetCode之All Possible Full Binary Trees(Kotlin)

    问题: A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list o ...

  2. Tweaked Identical Binary Trees - Medium

    Determine whether two given binary trees are identical assuming any number of 'tweak's are allowed. ...

  3. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

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

  5. C#LeetCode刷题之#617-合并二叉树​​​​​​​​​​​​​​(Merge Two Binary Trees)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...

  6. Given two binary trees, write a function to check if they areequal or not.

    Given two binary trees, write a function to check if they areequal or not. Two binary trees are cons ...

  7. (数据结构基础)Among the following threaded binary trees (the threads are represented by dotted curves),……

    当年学这些的时候真的是苦于没有人讲,现在复习考研,我会见到这种题就写下来,学弟学妹们欢迎点个关注,最近也在创业想实习的可以找我联系:没看懂的话是我表述有问题,欢迎指出和私戳. Among the fo ...

  8. Rosalind第88题:Counting Rooted Binary Trees

    Problem As in the case of unrooted trees, say that we have a fixed collection of  taxa labeling the  ...

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

最新文章

  1. 02搭建cdh版本控制
  2. python装饰器函数执行后日志_Python装饰器使用规范案例详解
  3. java怎么设置快速修复键_Java开发环境之------MyEclipse快捷键和排除错误第一选择ctrl+1(***重点***:ctrl+1,快速修复---有点像vs中的快速using...
  4. 你真的了解低代码平台吗?
  5. [Intel Edison开发板] 04、Edison开发基于nodejs和redis的服务器搭建
  6. YAML-学习笔记!
  7. C#调用mciSendString播放音频文件
  8. 俄罗斯被指为 SolarWinds 供应链事件元凶,技术公司受制裁,常用5大漏洞遭曝光...
  9. JSP听课笔记(一)
  10. Atitit 按照行读取文件 目录 1.1. 类库'readline' 1 1.2. 类库C:\0workspace\dataindexGenerNodejs\sdk\io\FileUtils.js
  11. 弱电总包施工组织设计与实施方案
  12. 原型工具Axure vs Mockplus ——表格对比 , 你选谁?
  13. unix网络编程中的fd是什么
  14. varnish php,php实现监控varnish缓存服务器的状态_PHP
  15. 03-Mono Flux操作
  16. 题解 CF174A 【Problem About Equation】
  17. [转载学习] 背包问题九讲
  18. github.com/spf13/viper go viper包介绍
  19. C语言探索之旅 | 第一部分第二课:工欲善其事,必先利其器
  20. lol登入服务器显示证书,登录LOL提示安全证书不可用怎么办

热门文章

  1. 软raid1 重新同步_盾构同步注浆施工,看完你就会!
  2. windows 将python源代码加密_对源代码进行pyd加密
  3. php表单传值哪几种方式,PHP表单传值的方式有哪些?
  4. 机器人售卖雪糕机_智能售货机售货机,冰淇淋厂
  5. linux目录为root所有文件,linux获取文件所有目录/文件夹的例子linux操作系统 -电脑资料...
  6. Linux怎么互相ping通,主机+虚拟机Ubuntu+开发板互相ping通
  7. python爬取抖音评论_怎样用Python3爬取抖音神曲
  8. HDLBits 系列(4)如何设计一定不会产生Latch的组合逻辑?
  9. 剖析云计算中的“共享型数据库”(转载)
  10. 教程-Delphi调用百度地图API(XE8+WIN7)