leetcode_894. All Possible Full Binary Trees
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相关推荐
- 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 ...
- Tweaked Identical Binary Trees - Medium
Determine whether two given binary trees are identical assuming any number of 'tweak's are allowed. ...
- 17. Merge Two Binary Trees 融合二叉树
[抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...
- 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 ...
- C#LeetCode刷题之#617-合并二叉树(Merge Two Binary Trees)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...
- 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 ...
- (数据结构基础)Among the following threaded binary trees (the threads are represented by dotted curves),……
当年学这些的时候真的是苦于没有人讲,现在复习考研,我会见到这种题就写下来,学弟学妹们欢迎点个关注,最近也在创业想实习的可以找我联系:没看懂的话是我表述有问题,欢迎指出和私戳. Among the fo ...
- 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 ...
- 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 ...
最新文章
- 02搭建cdh版本控制
- python装饰器函数执行后日志_Python装饰器使用规范案例详解
- java怎么设置快速修复键_Java开发环境之------MyEclipse快捷键和排除错误第一选择ctrl+1(***重点***:ctrl+1,快速修复---有点像vs中的快速using...
- 你真的了解低代码平台吗?
- [Intel Edison开发板] 04、Edison开发基于nodejs和redis的服务器搭建
- YAML-学习笔记!
- C#调用mciSendString播放音频文件
- 俄罗斯被指为 SolarWinds 供应链事件元凶,技术公司受制裁,常用5大漏洞遭曝光...
- JSP听课笔记(一)
- Atitit 按照行读取文件 目录 1.1. 类库'readline'	1 1.2. 类库C:\0workspace\dataindexGenerNodejs\sdk\io\FileUtils.js
- 弱电总包施工组织设计与实施方案
- 原型工具Axure vs Mockplus ——表格对比 , 你选谁?
- unix网络编程中的fd是什么
- varnish php,php实现监控varnish缓存服务器的状态_PHP
- 03-Mono Flux操作
- 题解 CF174A 【Problem About Equation】
- [转载学习] 背包问题九讲
- github.com/spf13/viper go viper包介绍
- C语言探索之旅 | 第一部分第二课:工欲善其事,必先利其器
- lol登入服务器显示证书,登录LOL提示安全证书不可用怎么办
热门文章
- 软raid1 重新同步_盾构同步注浆施工,看完你就会!
- windows 将python源代码加密_对源代码进行pyd加密
- php表单传值哪几种方式,PHP表单传值的方式有哪些?
- 机器人售卖雪糕机_智能售货机售货机,冰淇淋厂
- linux目录为root所有文件,linux获取文件所有目录/文件夹的例子linux操作系统 -电脑资料...
- Linux怎么互相ping通,主机+虚拟机Ubuntu+开发板互相ping通
- python爬取抖音评论_怎样用Python3爬取抖音神曲
- HDLBits 系列(4)如何设计一定不会产生Latch的组合逻辑?
- 剖析云计算中的“共享型数据库”(转载)
- 教程-Delphi调用百度地图API(XE8+WIN7)