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 ...
随机推荐
- linux学习笔记-(1)-安装
学习的第一部,当然是寻找学习资料,如今的网络如此发达,只要下点功夫,基本上能在网上找到一切自己所需要的东西,而且还是免费滴哟! ---------------------分割线------------ ...
- 转:WCF、WebAPI、WCFREST、WebService之间的区别
WCF.WebAPI.WCFREST.WebService之间的区别 注明:转载 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API ...
- Hdu 4081 最小生成树
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- 基于socket的客户端和服务端聊天简单使用 附Demo
功能使用 服务端 分离一个不停接受客户端请求的线程 接受不客户端请求的线程中,再分离就收消息的线程 几大对象分别是 IPEndPoint IP终结点 服务端Socket,绑定终结点Bind,启动监听L ...
- Difinition Of Done
A Story is Sprint ready (Rally Defined) when............. The story has well defined and testable ac ...
- 图形化查看maven的dependency依赖
开发项目的时候,我们想知道一个maven的依赖库是来自那个工程,eclipse有插件可以直接看Dependency Hierarchy,推荐一个第三方的工具yED 在工程的pom.xml文件中添加如下 ...
- 摸索探寻之Mac OS 使用及快捷键
刚发现的一个介绍Mac OS入门的帖子,新入手Mac的同学可以看看http://www.cnblogs.com/chijianqiang/archive/2011/08/03/2126593.html ...
- ACM:UESTC - 649 括号配对问题 - stack
UESTC - 649 括号配对问题 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu ...
- JavaScript必须了解的知识点总结【转】
整理的知识点不全面但是很实用. 主要分三块: (1)JS代码预解析原理(包括三个段落): (2)函数相关(包括 函数传参,带参数函数的调用方式,闭包): (3)面向对象(包括 对象创建.原型链,数据类 ...
- EF 5 最佳实践白皮书
Performance Considerations for Entity Framework 5 By David Obando, Eric Dettinger and others Publish ...