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

LeetCode 894. All Possible Full Binary Trees

原题链接在这里:https://leetcode.com/problems/all-possible-full-binary-trees/

题目:

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

题解:

If we mark node from 0 to N-1.

Only the odd index node, like 1, 3, 5 could be root. Since count of nodes on left is odd and count of node on right is odd.

Even number of nodes can't form a full binary tree.

Could use recursive call to get a list of possible left subtrees, and a list of possible right subtrees.

For each of them, append it back to root, and add to res.

We could use cache to memorize results of N. Then no need to recalculate. 

Also it would be bebetter to deep clone the subtree, or in the future if one node is changed, all its reference would be changed too.

Time Complexity: O(n^3). T(N) = T(1)*T(N-2) + T(3)*T(N-4) + ... + T(N-2)*T(1). With Cache, when calculate T(N-2), all number smaller than N-2 is calculated and put into cache. Then for the rest, get T(i) from cache needs O(1) time. There could be O(N) results for subtrees, size of list could be O(N). Totoally, equation length is N/2, Then T(N) = N/2 * (N*N) = O(N^3).

Space: O(N^3). cache count is O(N). For each value, list size could be O(N). Within list, each tree could be O(N). Thus, it takes O(N^3) space.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     HashMap<Integer, List<TreeNode>> hm = new HashMap<Integer, List<TreeNode>>();
12     
13     public List<TreeNode> allPossibleFBT(int N) {
14         if(hm.containsKey(N)){
15             return hm.get(N);
16         }
17         
18         List<TreeNode> res = new ArrayList<TreeNode>();
19         if(N<1 || N%2 == 0){
20             hm.put(N, res);
21             return res;
22         }
23         
24         if(N == 1){
25             res.add(new TreeNode(0));
26             hm.put(N, res);
27             return res;
28         }
29         
30         for(int i = 1; i<N; i+=2){
31             List<TreeNode> left = allPossibleFBT(i);
32             List<TreeNode> right = allPossibleFBT(N-1-i);
33 
34             for(TreeNode l : left){
35                 for(TreeNode r : right){
36                     TreeNode root  = new TreeNode(0);
37                     root.left = deepClone(l);
38                     root.right = deepClone(r);
39                     res.add(root);
40                 }
41             }
42         }
43         
44         hm.put(N, res);
45         return res;
46     }
47     
48     private TreeNode deepClone(TreeNode root){
49         if(root == null){
50             return root;
51         }
52         
53         TreeNode copy = new TreeNode(root.val);
54         copy.left = deepClone(root.left);
55         copy.right = deepClone(root.right);
56         return copy;
57     }
58 }

类似Unique Binary Search Trees II.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11096312.html

相关文章:

  • git预览项目
  • How does SGA/PGA allocate on AMM?
  • django考点
  • linux应用系统日志
  • jQuery-对Select的操作集合[终结篇]
  • Mldonkey端口映射获取High id
  • 10 DOM文档对象模型
  • 详细注释GetFATEntry(转载)
  • webpack3 打包
  • web项目部署到Tomcat服务器的三种方式
  • 2011年5月16日
  • Oracle数据库---存储过程、存储函数
  • flash特效原理:图片切换滚动
  • SPAN
  • C# 进程 与 线程
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • leetcode98. Validate Binary Search Tree
  • MaxCompute访问TableStore(OTS) 数据
  • ucore操作系统实验笔记 - 重新理解中断
  • Vue 重置组件到初始状态
  • 搭建gitbook 和 访问权限认证
  • 给Prometheus造假数据的方法
  • 技术发展面试
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 详解移动APP与web APP的区别
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • 湖北分布式智能数据采集方法有哪些?
  • ​Java并发新构件之Exchanger
  • (2022 CVPR) Unbiased Teacher v2
  • (31)对象的克隆
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (6)添加vue-cookie
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (LeetCode 49)Anagrams
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (一)Neo4j下载安装以及初次使用
  • ... 是什么 ?... 有什么用处?
  • .bashrc在哪里,alias妙用
  • .java 指数平滑_转载:二次指数平滑法求预测值的Java代码
  • .NET 设计模式初探
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • [ IOS ] iOS-控制器View的创建和生命周期
  • [C#]手把手教你打造Socket的TCP通讯连接(一)
  • [C++核心编程](四):类和对象——封装
  • [CVPR2021]Birds of a Feather: Capturing Avian Shape Models from Images
  • [halcon案例2] 足球场的提取和射影变换
  • [Intel Edison开发板] 05、Edison开发基于MRAA实现IO控制,特别是UART通信