【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 ...
随机推荐
- ES6深入浅出-13 Proxy 与 Reflect-1.Reflect 反射
阮一峰 http://es6.ruanyifeng.com/#docs/reflect MDN有一些简陋的介绍 https://developer.mozilla.org/zh-CN/docs/We ...
- Qt编写自定义控件68-IP地址输入框
一.前言 这个IP地址输入框控件,估计写烂了,网上随便一搜索,保证一大堆,估计也是因为这个控件太容易了,非常适合新手练手,一般的思路都是用4个qlineedit控件拼起来,然后每个输入框设置正则表达式 ...
- ABAP语法篇2 内表操作语法
VALUE MOVE-CORRESPONDING CORRESPOING FOR REDUCE GROUP BY FILTER VALUE语法: 结构赋值 ... VALUE dtype | #( ...
- gen语言
概率编程语言(PPL)领域正经历着机器学习技术快速发展带来的奇迹般的复兴.在短短的几年里,PPL 已经从一个模糊的统计研究领域发展出十几个活跃的开源方案.最近,麻省理工学院(MIT)的研究人员推出了一 ...
- redis和memcache对比
1.性能方面:没有必要过多的关心性能,因为二者的性能都已经足够高了.由于Redis只使用单核,而Memcached可以使用多核,所以在比较上,平均每一个核上Redis在存储小数据时比Memcached ...
- Go 包导入备忘
一 包的导入语法 在写Go代码的时候经常用到import这个命令用来导入包文件,看到的方式参考如下: import( "fmt" ) ...
- tp 执行sql 语句
$User = D('User'); $User->query('select * from think_user where status=1'); $User->execute('up ...
- 组件文档系统-mdx-react-docz
推荐指数:
- Ubuntu开发环境配置
主要是: 源的更新 安装vim编辑器 远程登录xrdp相关配置 synergy symless键鼠共享配置 对新买的硬盘进行格式化和分区 vsftp环境搭建 gcc开发环境配置 qt5开发环境配置 m ...
- 2019年春季学期《C语言程序设计II》课程总结
2019年春季学期<C语言程序设计II>课程总结 1.课程情况 教学内容 课堂小结 作业安排 优秀作业 备注 1.开学谈心 2.测验数据类型.运算符与表达式的自学情况,并讲解测验题目3.第 ...