【leetcode】366.Find Leaves of Binary Tree
原题
Given a binary tree, collect a tree's nodes as if you were doing this:
Collect and remove all leaves, repeat until the tree is empty.
Example:
Given binary tree
1
/
2 3
/
4 5
Returns [4, 5, 3], [2], [1].
Explanation:
- Removing the leaves [4, 5, 3] would result in this tree:
1
/
2 - Now removing the leaf [2] would result in this tree:
1 - Now removing the leaf [1] would result in the empty tree:
[]
Returns [4, 5, 3], [2], [1].
解析
给一颗二叉树,
返回这个二叉树的叶子节点,每一次一组,依次放在List中
思路
我的思路:每一次深度遍历收割一次叶子放在结果中(缺点:深度有多少,就要深度遍历多少次)
优化算法:深度优选搜索,叶子节点深度标记为0,其他节点的深度标记为左右叶子节点较大的深度+1;相同深度的放在一个结果节点中
我的解法
public List<List<Integer>> FindLeaves(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
while (root != null) {
List<Integer> re = new ArrayList<>();
if (DFSTree(root, re)) {
root = null;
}
result.add(re);
}
return result;
}
private boolean DFSTree(TreeNode root, List<Integer> re) {
if (root.left == null && root.right == null) {
re.add(root.val);
return true;
}
if (root.left != null) {
if (DFSTree(root.left, re)) {
root.left = null;
}
}
if (root.right != null) {
if (DFSTree(root.right, re)) {
root.right = null;
}
}
return false;
}
最优解
public List<List<Integer>> FindLeavesOptimize(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
DFSTreeOptimize(root, result);
return result;
}
private int DFSTreeOptimize(TreeNode root, List<List<Integer>> result) {
if (root.left == null && root.right == null) {
if (result.size() <= 0) {
result.add(0, new ArrayList<>());
}
result.get(0).add(root.val);
return 0;
}
int deep, leftDeep = 0, rightDeep = 0;
if (root.left != null) {
leftDeep = DFSTreeOptimize(root.left, result);
}
if (root.right != null) {
rightDeep = DFSTreeOptimize(root.right, result);
}
deep = Math.max(leftDeep, rightDeep) + 1;
if (result.size() <= deep) {
result.add(deep, new ArrayList<>());
}
result.get(deep).add(root.val);
return deep;
}
【leetcode】366.Find Leaves of Binary Tree的更多相关文章
- 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- 【LeetCode】104 - Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【LeetCode】111 - Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- 【DataBase】H2 DateBase与项目集成
本例介绍H2与web项目的集成 项目启动H2数据库 1.新建Maven Web项目,参考:[Maven]Eclipse 使用Maven创建Java Web项目 2.引入h2的jar包依赖 <de ...
- (NSNumber **)value和(NSNumber * __autoreleasing *)value
今天在看别人开源项目的时候看到这样的代码: 正文从这里开始~~~ 定义如下: /** 评论详情页基础设置 @param BaseSettingBlock 基础设置 */ - (void)setUpCo ...
- css代码陷阱
1.选择器优先级 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- C# 利用bat文件轻松创建windos 服务
最近,一个项目需要一个后台服务,定时去读取数据,这是直接创建一个bat文件,双击执行就可以了,为了省事哦 主要分两个步奏 1.创建windows服务的应用程序.这一点不做过多讲解.网上有太多的例子 2 ...
- upload-labs 上传漏洞靶场环境以及writeup
一个帮你总结所有类型的上传漏洞的靶场 https://github.com/c0ny1/upload-labs 靶场环境(基于phpstudy这个php集成环境) https://github.com ...
- 数据挖掘经典算法PrefixSpan的一个简单Python实现
前言 用python实现了一个没有库依赖的"纯" py-based PrefixSpan算法. Github 仓库 https://github.com/Holy-Shine/Pr ...
- Jetty的安装和配置
Jetty 是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...
- LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...
- LocalDate LocalTime LocalDateTime Instant的操作与使用
一.简介 LocalDate表示当前(或指定)日期,格式为:yyyy-MM-dd LocalTime表示当前(或指定)时间,格式为:HH:mm:ss SSS LocalDateTime表示当前(或指定 ...
- [转帖]Dockerfile: ENTRYPOINT和CMD的区别
Dockerfile: ENTRYPOINT和CMD的区别 https://zhuanlan.zhihu.com/p/30555962 在我们查阅Dockerfile的官方文档时, 有可能发现一些命令 ...