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 ...
最新文章
- 激光雷达和毫米波雷达
- Maven-EclipseEE使用Maven构建Java web项目从0到1
- Spine学习六 - 碰撞检测
- scroll-view 横向滑动无效的问题
- d06调试详细说明_PIC系列单片机配置字详细介绍(长期实践总结的压箱底资料)
- windowns定时关机命令
- 单片机万年历阴阳历c语言,51系列单片机的一种万年历优化算法源程序
- js清空浏览器cokie缓存_JS设置cookie,删除cookie
- Android音频数据格式概述
- android 屏蔽电源键,home键
- [HNOI2018]毒瘤
- switch怎么切换服务器账号,任天堂eshop如何换区 switch账号如何切换其他服地区
- 浅谈区块链技术对未来行业的影响
- NTC转0-10V/0-5V热敏电阻转模拟信号温度变送器/温度变送器Cu50 PT100热电阻K型S型电偶NTC转4-20mA 0-10V模拟量NTC热敏电阻转电压10K NTC转0-5V0-10V
- 【涨知识】国内外点云处理著名研究团队
- 从CRUD到自身架构师的蜕变之路?全靠这份:Java中高级核心知识全面解析
- MySQL运行原理与基础架构
- 沟通的艺术II:看入人里 之知觉
- 制作唐诗网页代码_唐诗宋词网页制作素材
- 主题:分享一个靠谱的免费论文查重网站
热门文章
- python知网查重_用Python写了个检测抄袭/文章去重算法(nshash)
- 自定义关机计算机,在win7系统中自定义设置关机壁纸教程介绍
- 微型计算机怎么插耳机,让听音更轻松一些,朗琴E10真无线耳机
- 图书管理系统python代码课程设计报告_数据结构图书管理系统课程设计报告
- java编写类骑士游历_java课程设计骑士游历程序的开发
- centos mysql数据迁移_Mysql 5.7.17 离线版安装和数据迁移(centos 7)
- LINUX应用修改硬件寄存器l,郝健: Linux内存管理学习笔记-第2节课【转】
- python调用math函数log_Python中求对数方法总结
- 完美设置“Word表格中文字上下居中”
- HDLBits 系列(26)独热码有限状态机实现的两种方式