原题

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:

  1. Removing the leaves [4, 5, 3] would result in this tree:

    1

    /

    2
  2. Now removing the leaf [2] would result in this tree:

    1
  3. 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的更多相关文章

  1. 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  4. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  5. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  6. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  7. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  8. 【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 ...

  9. 【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 ...

随机推荐

  1. [译]在Pandas的Dataframe中删除行、列

    导入模块 import pandas as pd 创建dataframe data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 'yea ...

  2. Qt编写气体安全管理系统16-云端同步

    一.前言 云端同步功能是为了后期的拓展做准备的,他的目的就是将本地的数据库中的记录,比如实时采集到的数据以及存储的运行记录等,同步到云端数据库上,默认采用阿里云的mysql数据库,阿里云速度还是挺快的 ...

  3. Golang中的Map(集合)

    Map 是一种无序的键值对的集合.Map 最重要的一点是通过 key 来快速检索数据,key 类似于索引,指向数据的值. Map 是一种集合,所以我们可以像迭代数组和切片那样迭代它.不过,Map 是无 ...

  4. 【GStreamer开发】GStreamer播放教程02——字幕管理

    目标 这篇教程和上一篇非常相似,但不是切换音频流,而是字幕了.这次我们会展示: 如何选择选择字幕流 如何引入外部的字幕 如何客制化字幕使用的字体 介绍 我们都知道一个文件可以有多个音视频流并且可以使用 ...

  5. laravel 运用.env部署多环境配置

    切换到laravel 入口文件bootstrap\app.php 在  return $app;  之上添加: $environmentPath = $app->environmentPath( ...

  6. jenkins publish .net core application to linux server in docker

    上一个Demo进行了单独的Jenkins远程部署, 本Demo将使用流行的Jenkins+Git+Docker进行持续部署. 准备Linux服务器 和上一篇Demo一样, 在Azure创建一台Cent ...

  7. Linux基础-03-用户、群组

    1. 用户及passwd文件 1) /etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) /etc/passwd文件每个字段的具体含义: ...

  8. page分页问题,根据页码获取对应页面的数据,接口调用

    添加一个log.js文件,进行接口调用. import axios from '@/libs/api.request' const MODULE_URL = '/log'; export const ...

  9. es6☞对象的解构赋值

    变量必须与属性同名 let {name, age} = {name: 'wang', age: 22}; console.log(name, age); //wang 22 let {name} = ...

  10. Eclipse RCP使用SWT.EMBEDDED方式显示batik的svgCanvas后窗口最大化变白问题

    // 设置svg组件一直动态(这一行代码导致了最大化变白的问题)svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);// 以下两行解决最大化变白 ...