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 ...
随机推荐
- java多线程面试题
很多核心Java面试题来源于多线程(Multi-Threading)和集合框架(Collections Framework),理解核心线程概念时,娴熟的实际经验是必需的.这篇文章收集了Java线程方面 ...
- zorka源码解读之通过beanshell进行插桩的流程
zorka中插桩流程概述 1.在SpyDefinition中配置插桩属性,将SpyDefinition实例提交给插桩引擎.2.SpyDefinition实例中包含了插桩探针probes,probe插入 ...
- PHPExcel——读取excel
在网上找了excel读取的一些资料,个人觉得PHPExcel这还是挺好用的,相对比较全的工具. 主要功能是读取上传的excel文件,插入或更新到数据库. iconv("gbk",& ...
- js模块开发(一)
现在嵌入页面里面的javascript代码越来越复杂,于是可能依赖也越来越严重,使用别人开发的js也越来越多,于是在理想情况下,我们只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. 于是j ...
- linux 内核cache
写驱动总会碰到和cache相关的东西 记录下用到的接口: 驱动中用的内存地址一般为内核地址,用户调用驱动接口时,有时候会把自己申请的地址赋给驱动,此时用户kmalloc得到内核地址, 再用mmap获得 ...
- tableview 位置发生偏移
状况描述:1.首次进入该界面时正常 2.push了新的界面后,再返回该界面 tableview和导航栏直接出现了间隔区域 tableview为代码创建 _tableView = [[UITableV ...
- HTML常用属性
blue:蓝色 red:红色 yellow:黄色 green:绿色 white:白色 gray:灰色 /*去掉下划线*/ text-decoration: none; /*添加下划线*/ te ...
- VS2013 配置pthread
参考:http://blog.csdn.net/qianchenglenger/article/details/16907821 一.下载地址 ftp://sourceware.org/pub/pth ...
- 更换app开发者账号
在开源中国上面有一个答案,http://www.oschina.net/question/2307266_237220 下面是我的执行步骤 首先在iTunes Connect中找到要更换开发者账号的a ...
- bzoj2243: [SDOI2011]染色--线段树+树链剖分
此题代码量较大..但是打起来很爽 原本不用lca做一直wa不知道为什么.. 后来改lca重打了一遍= =结果一遍就AC了orz 题目比较裸,也挺容易打,主要是因为思路可以比较清晰 另:加读入优化比没加 ...