【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 ...
随机推荐
- kafka删除topic后再创建同名的topic报错(ERROR org.apache.kafka.common.errors.TopicExistsException)
[hadoop@datanode3 logs]$ kafka-topics.sh --delete --zookeeper datanode1:2181 --topic firstTopic firs ...
- rename批量命名命令
参考:http://www.ttlsa.com/linux-command/linux-rename-command-batch-rename/ Linux下rename命令有两种版本一种是C语言版本 ...
- openpyxl,xlrd,win32com,wxpython,logging
目录 一. openpyxl常用操作总结 二. xlrd常用操作总结 三. win32com常用操作总结 四. 自定义异常 五. 判断中文 六. Excel数字字母转换 七. 使用wxpython进行 ...
- 实验1 C 语言开发环境使用和数据类型、运算符、表达式
# include <stdio.h> int main() { int x; printf("x:\n"); scanf("%d",&x) ...
- 【计算机视觉】目标检测之ECCV2016 - SSD Single Shot MultiBox Detector
本文转载自: http://www.cnblogs.com/lillylin/p/6207292.html SSD论文阅读(Wei Liu--[ECCV2016]SSD Single Shot Mul ...
- 【VS开发】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)
1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...
- Appium移动自动化测试-----(六)4.运行第一个Appium脚本
新建maven空白工程 前置条件:安装eclipse或IntelliJ IDEA,及其maven插件,请自行百度 新建的工程如下: 新建目录apps,并将下载的安装包,拷贝到该目录下 打开POM增加依 ...
- [学习笔记] 下载、安装、启动 Eclipse(OEPE)
OEPE 的全称是: Oracle Enterprise Pack for Eclipse 截止目前的最新版本是:(12.2.1.9) 下载地址:(需要在甲骨文官网注册一个账户才能下载) https: ...
- 《Mysql - Mysql 是如何保证高可用的?》
一:为什么要进行主备切换? - 比如软件升级.主库所在机器按计划下线等.主动运维. - 比如主库所在机器掉电,为了保证服务的正常运行,而进行切换.可能是被动操作. - 流程图 - 二:什么是同步延 ...
- Ribbon【自定义客户端】
Ribbon的加载策略是懒加载,即第一次请求的时候才加载对应上下文,正是这个原因,很多时候第一次调用显得很慢,甚至会超时,所以,可以通过指定ribbon具体服务名称来开启饿加载,即在工程启动的时候,加 ...