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 == )
- {
- TreeNode* rt = new TreeNode();
- ret.push_back(rt);
- return ret;
- }
- for(int i=; i<=(N-)/; i+=) //左子树的节点数
- {
- vector<TreeNode*> left = allPossibleFBT(i); //创建所有可能左子树
- vector<TreeNode*> right = allPossibleFBT(N--i); //创建所有可能的右子树
- for(int j=;j<left.size();j++) //遍历所有左子树
- for(int k=;k<right.size();k++) //遍历所有右子树
- {
- TreeNode * rt = new TreeNode(); //创建根节点
- rt->left = left[j];
- rt->right = right[k];
- ret.push_back(rt);
- if(i != N--i) //如果左右子树节点数不同,交换左右子树也是一种可能
- {
- TreeNode * rt2 = new TreeNode();
- rt2->left = right[k];
- rt2->right = left[j];
- ret.push_back(rt2);
- }
- }
- }
- return ret;
- }
- };
leetcode_894. All Possible Full Binary Trees的更多相关文章
- hdu3240 Counting Binary Trees
Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Binary Trees
1. Definiation What is Binary Trees? Collection of node (n>=0) and in which no node can have more ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 每天进步一点点—mysql-mysqldump
一. 简单介绍 mysqldump是client用来备份数据库或者在不通数据库之间进行数据迁移的工具,备份内容包括创建表或者装载表的SQL语句 二. 命令格式 备份单个数 ...
- [10.26_P2] 最短路 (单源最短路应用)
单源最短路问题拓展 Description 给你一张图,图上有 n 个点,m 条边,要你找到两个点,使其最短路恰好包含给定的 k 个点.输出这条最短路的长度,输入保证有解. 输入格式 第一行两个数 n ...
- Iterator 使用
Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactor ...
- POJ - 3352 Road Construction(边双连通分支)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2.POJ - 3177 Redundant Paths(边双连通分支)(模板) 与这道题一模一样.代码就改了下范围,其他都没动 ...
- codeforces 686A A. Free Ice Cream(水题)
题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...
- Educational Codeforces Round 22 补题 CF 813 A-F
A The Contest 直接粗暴贪心 略过 #include<bits/stdc++.h> using namespace std; int main() {//freopen(&qu ...
- bzoj4031
4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 823 Solved: 407[Submit][Statu ...
- 创建swagger的springboot-stater,并在spring cloud zuul网关中引入
Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...
- Luogu P3619 魔法 【贪心/微扰证明】
题目描述 cjwssb知道是误会之后,跟你道了歉.你为了逗笑他,准备和他一起开始魔法.不过你的时间不多了,但是更惨的是你还需要完成n个魔法任务.假设你当前的时间为T,每个任务需要有一定的限制ti表示只 ...
- eurekaclient向eurekaserver注册使用真实ip设置
有时候eureka.instance.prefer-ip-address=true不管用,解决办法如下.