Leetcode: 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:
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].
Better Solution: https://discuss.leetcode.com/topic/49194/10-lines-simple-java-solution-using-recursion-with-explanation/2
For this question we need to take bottom-up approach. The key is to find the height of each node. The height of a node is the number of edges from the node to the deepest leaf.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
helper(root, res);
return res;
} public int helper(TreeNode cur, List<List<Integer>> res) {
if (cur == null) return -1;
int level = 1 + Math.max(helper(cur.left, res), helper(cur.right, res));
if (res.size() <= level)
res.add(new ArrayList<Integer>());
res.get(level).add(cur.val);
cur.left = cur.right = null;
return level;
}
}
First time solution: HashSet+ DFS
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null) return res;
HashSet<TreeNode> visited = new HashSet<>();
while (!visited.contains(root)) {
ArrayList<Integer> leaves = new ArrayList<Integer>();
helper(root, leaves, visited);
res.add(new ArrayList<Integer>(leaves));
}
return res;
} public void helper(TreeNode cur, ArrayList<Integer> leaves, HashSet<TreeNode> visited) {
if ((cur.left==null || visited.contains(cur.left)) && (cur.right==null || visited.contains(cur.right))) {
leaves.add(cur.val);
visited.add(cur);
return;
}
if (cur.left!=null && !visited.contains(cur.left))
helper(cur.left, leaves, visited);
if (cur.right!=null && !visited.contains(cur.right))
helper(cur.right, leaves, visited);
}
}
Leetcode: 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, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- 【一天一道LeetCode】#106. Construct Binary Tree from Inorder and Postorder Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
随机推荐
- git stash和git stash pop
git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug, 先stash, 使返回到自己上一个comm ...
- letter upper lower combo
以前同事为了炫耀ruby的简洁,特意出一道题来考小陈: 在写一个爆破密码的字典生成工具,其中有这样一个需求: 输入一个单词:列出这个单词的所有大小写组合,比如ruby Ruby rUby ruBy r ...
- 踩坑事件:不能对基于文本的临时表使用sql insert语句
先来描述一下问题: 如果你是从基于文本的数据源来创建DataFrame的,当你将DataFrame注册为临时表后,如果对这个临时表进行insert into 操作,会抛出异常的. 问题答案参见:htt ...
- js性能优化-事件委托
js性能优化-事件委托 考虑一个列表,在li的数量非常少的时候,为每一个li添加事件侦听当然不会存在太多性能方面的问题,但是当列表非常的长,长到上百上千甚至上万的时候(当然只是一个解释,实际工作中很少 ...
- mysql数据库安装及使用
前言:本文为在ubuntu系统下使用mysql数据库,mysql 版本为:Ver 14.14 Distrib 5.5.43 (mysql版本可在命令行中输入mysql --version显示) 一.m ...
- 了解学习JS中this的指向
[转] 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问 ...
- [IOS] 利用@IBInspectable
某些uiview中设置 这个关键字 IBInspectable 可以让其设置的属性,在右侧的属性栏目里面进行直接设置, 这是最近看了一下wwdc的一个视频学习到的,可以方便的进行 UI的测试,
- 从头到尾彻底理解KMP
从头到尾彻底理解KMP 作者:July 时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个多月不断反复改进. 1. 引言 本KMP原文最初写于2年多前的201 ...
- HTML5表单
1.placeholder placeholder="e.g. King Kong" 只需在input元素中加入placeholder属性,其属性值就会默认显示为占位符文字,输入框 ...
- 【偶像大师 白金星光】的【Variable Tone】技术大公开!偶像从哪里看都那么可爱,VA小组谈制作方针
http://game.watch.impress.co.jp/docs/news/1016369.html 自从街机版的运营依赖,今年迎来了[偶像大师]系列的11周年.在CEDEC ...