[LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty.
Example:
Given binary tree
1
/ \
2 3
/ \
4 5
Returns [4, 5, 3], [2], [1].
Explanation:
1. Remove the leaves [4, 5, 3] from the tree
1
/
2
2. Remove the leaf [2] from the tree
1
3. Remove the leaf [1] from the tree
[]
Returns [4, 5, 3], [2], [1].
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
给一个二叉树,找出它的叶节点然后删除,重复此步骤,直到二叉树为空。
The key to solve this problem is converting the problem to be finding the index of the element in the result list. Then this is a typical DFS problem on trees.
Java:
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
helper(result, root);
return result;
}
// traverse the tree bottom-up recursively
private int helper(List<List<Integer>> list, TreeNode root){
if(root==null)
return -1;
int left = helper(list, root.left);
int right = helper(list, root.right);
int curr = Math.max(left, right)+1;
// the first time this code is reached is when curr==0,
//since the tree is bottom-up processed.
if(list.size()<=curr){
list.add(new ArrayList<Integer>());
}
list.get(curr).add(root.val);
return curr;
}
Python:
class Solution(object):
def findLeaves(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
def findLeavesHelper(node, result):
if not node:
return -1
level = 1 + max(findLeavesHelper(node.left, result), \
findLeavesHelper(node.right, result))
if len(result) < level + 1:
result.append([])
result[level].append(node.val)
return level result = []
findLeavesHelper(root, result)
return result
C++:
// Time: O(n)
// Space: O(h) /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> result;
findLeavesHelper(root, &result);
return result;
} private:
int findLeavesHelper(TreeNode *node, vector<vector<int>> *result) {
if (node == nullptr) {
return -1;
}
const int level = 1 + max(findLeavesHelper(node->left, result),
findLeavesHelper(node->right, result));
if (result->size() < level + 1){
result->emplace_back();
}
(*result)[level].emplace_back(node->val);
return level;
}
};
类似题目:
[LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
[LeetCode] 310. Minimum Height Trees 最小高度树
[LeetCode] 545. Boundary of Binary Tree 二叉树的边界
All LeetCode Questions List 题目汇总
[LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点的更多相关文章
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [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 ...
- LeetCode 366. Find Leaves of Binary Tree
原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...
- 【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 lea ...
- 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 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 ...
- 366. Find Leaves of Binary Tree C#
Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing th ...
- 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 ...
- 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 ...
随机推荐
- 如果在使用谷歌的gson的时候,在返回时间类型的数据的时候,
可能会出现在long类型的时间后面多3个0 如下图所示 可以自己创建一个json序列化的类 public class Date2LongSerializer extends JsonSerialize ...
- sql语句的面试题
中科软的面试题:http://www.mayiwenku.com/p-5888480.html 中科软的面试题:https://blog.csdn.net/woolceo/article/detail ...
- python完成加密参数sign计算并输出指定格式的字符串
加密规则: 1.固定加密字符串+字符串组合(key/value的形式,并通过aissc码排序), 2.通过sha1算法对排序后的字符串进行加密, 3.最终输出需要的参数sign 4.完成请求参数数据的 ...
- 10.29-10.30Test
10.29-10.30Test 查看请点个赞 转载请注明出处(~不然~) 题目 描述 做法 \(BSOJ5161\) 从小到大放入\(n\)个数,每次他可以覆盖没被覆盖且小于等于自己的数,求每个数覆盖 ...
- Mybatis02
1.mybatis动态sql foreach 添加接口方法 编写BookVo类 BookMapper.xml 测试 结果 2.模糊查询 3.查询返回结果集 resultMap:适合使用返回值是自定 ...
- linux命令之------Linux文档编辑
1.Vi和vim三种模式 (1)命令模式:移动光标 (2)插入模式:编辑文档 (3)末行模式:保存退出 不同模式操作示意图: 其中wq是保存退出,wq!强制保存退出:q不保存退出:q!强制不保存退出. ...
- Spark-Streaming kafka count 案例
Streaming 统计来自 kafka 的数据,这里涉及到的比较,kafka 的数据是使用从 flume 获取到的,这里相当于一个小的案例. 1. 启动 kafka Spark-Streaming ...
- 四种CSS样式的引入方式
准备 1.首先准备一个html文件:test.html,不建议使用记事本创建文件,建议使用Notepad++来创建并编辑文件,注意编码格式为:以UTF-8无BOM格式编码,否则会出现中文乱码,内容如下 ...
- 升级项目版本:SpringBoot1.5.x到SpringBoot2.0.x
1.升级版本的选择 首先去spring的官网看一下最新的版本与版本之间的依赖
- python 之实现断点下载与下载进度条
一.效果图 二.进度条代码 __author__ = 'Yang' import os import time from threading import Thread '''下载进度条''' cla ...