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:
Removing the leaves [4, 5, 3] would result in this tree:
1
/2
Now removing the leaf [2] would result in this tree:
1
Now removing the leaf [1] would result in the empty tree:
[]
Returns [4, 5, 3], [2], [1].
class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}; class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> listAll = new ArrayList<List<Integer>>();
while (root != null) {
List<Integer> list = new ArrayList<Integer>();
root = helper(list, root);
listAll.add(new ArrayList<Integer>(list));
} return listAll;
} private TreeNode helper(List<Integer> list, TreeNode root) {
if (root == null)
return null; if (root.left == null && root.right == null) {
list.add(root.val);
return null;
} root.left = helper(list, root.left);
root.right = helper(list, root.right); return root;
}
}
下面这种方法是不会移除leaves的。
/**
* 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<>();
height(root, res);
return res;
}
private int height(TreeNode node, List<List<Integer>> res){
if(null==node) return ;
int level = + Math.max(height(node.left, res), height(node.right, res));
if(res.size() == level - ) res.add(new ArrayList<>());
res.get(level - ).add(node.val);
return level;
}
}
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: 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
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]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, 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 lea ...
随机推荐
- grid-css
.fil-container { width: 100%; max-width: 75rem; margin-right: auto; margin-left: auto; padding-left: ...
- window.parent ,window.top,window.self 详解
在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...
- Android如何让真机显示debug log的调试信息
真机默认是不开启debug log调试功能的,以前我一直用模拟器,模拟器默认是开启debug log调试功能的,那么如何让真机开启呢? 我用华为Ascend P6为例: 1.进入拨号界面,输入*#*# ...
- 【poj3537】 Crosses ans Crosses
poj.org/problem?id=3537 (题目链接) 题意 给出一个1*n的棋盘,每次可以选择一个没被标记过的点打标记,若经过某一步操作使得出现3个连续的标记,则最后操作的人获胜.问是否存在先 ...
- UVA11400照明系统设计&& POJ1260Peals(DP)
紫书P275: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/A POJ http://poj.org/pr ...
- 得分(Score,ACM/ICPC Seoul 2005,UVa 1585)
#include<stdio.h> int main(void) { char b; int t,cou,sum; scanf("%d",&t); getcha ...
- NYOJ 77 开灯问题
#include <stdio.h> #include <string.h> #define maxn 1050 int a[maxn]; int main(void) { i ...
- 基于.NET的大型Web站点StackOverflow架构分析(转)
Stack Overflow网址:http://stackoverflow.com/ 当前访问量:每月9500PV(每天300多万PV) 当前Alexa排名:149 所用.NET技术:C#.Visua ...
- php一些常用函数的理解
mysql_result($res, $row, [$field=0])是获取查询结果集中的 某一个 单元的内容. 其中, $row是行偏移, $field是列偏移, 或者叫索引, 都是从0开始的. ...
- 使用Angular和Nodejs搭建聊天室
一,利用Node搭建静态服务器 这个是这个项目的底层支撑部分.用来支持静态资源文件像html, css, gif, jpg, png, javascript, json, plain text等等静态 ...