当前位置: 首页 > news >正文

leetcode讲解--894. 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 all possible full binary trees with N nodes. Each element of the answer is the root node of one possible tree.

Each node of each tree in the answer must have node.val = 0.

You may return the final list of trees in any order.

Example 1:

Input: 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
Explanation:

Note:

  • 1 <= N <= 20

题目地址

讲解

这道题太适合用来训练递归了。与以前做过的题不同,这里的返回值是一个list。而且还需要对树比较了解。最好是先画出N=1到5的时候,树的结构,然后会发现一些规律。注意下i+=2,步长是2。

Java代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<TreeNode> allPossibleFBT(int N) {
        List<TreeNode> result = new ArrayList<>();
        if(N%2==0){
            return result;
        }
        if(N==1){
            result.add(new TreeNode(0));
            return result;
        }
        for(int i=1;i<N;i+=2){
            for(TreeNode l:allPossibleFBT(i)){
                for(TreeNode r:allPossibleFBT(N-1-i)){
                    TreeNode root = new TreeNode(0);
                    root.left = l;
                    root.right = r;
                    result.add(root);
                }
            }
        }
        return result;
    }
}

相关文章:

  • React降级配置及Ant Design配置
  • 解决iOS10的Safari下Meta设置user-scalable=no无效的方法
  • 中国智慧城市“热战”的2018
  • django之中间件及CSRF跨站请求伪造-68
  • Javascripit类型转换比较那点事儿,双等号(==)
  • Win7 64位 Hadoop单机模式安装
  • 技术发展面试
  • Android开发者必备:推荐一款助力开发的开源APP
  • 关于for循环的简单归纳
  • MongoDB介绍
  • call apply 和 bind
  • PHP如何在CLI命令模式下连接Postgresql
  • tp5.1 路由获取参数问题总结
  • PIE SDK自定义滤波
  • 关于一对一视频聊天系统的那些干货必备知识
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • 4. 路由到控制器 - Laravel从零开始教程
  • chrome扩展demo1-小时钟
  • Java方法详解
  • laravel 用artisan创建自己的模板
  • MySQL的数据类型
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • opencv python Meanshift 和 Camshift
  • orm2 中文文档 3.1 模型属性
  • PHP的类修饰符与访问修饰符
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 分类模型——Logistics Regression
  • 计算机在识别图像时“看到”了什么?
  • 数据仓库的几种建模方法
  • 我的业余项目总结
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 智能合约开发环境搭建及Hello World合约
  • 阿里云服务器购买完整流程
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • ​520就是要宠粉,你的心头书我买单
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (推荐)叮当——中文语音对话机器人
  • @SuppressWarnings注解
  • [ C++ ] template 模板进阶 (特化,分离编译)
  • [ solr入门 ] - 利用solrJ进行检索
  • [.net] 如何在mail的加入正文显示图片
  • [<事务专题>]
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [C++] sqlite3_get_table 的使用
  • [C++]类和对象【上篇】
  • [CDOJ 1343] 卿学姐失恋了
  • [fsevents@^2.1.2] optional install error: Package require os(darwin) not compatible with your platfo
  • [go 反射] 进阶
  • [linux学习]apt-get参数解析