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:
1Now 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 ...
随机推荐
- oracle 的安装脚本
==[root@oracle ~]# cat 1.preusers.sh ==#!/bin/bash#Purpose:Create 3 groups named 'oinstall','dba','o ...
- jQuery1.4源码解读
来吧, 慢慢折腾吧 总结一下: jq1.4挺简单的, 正则写的不多, 看的都懂, 多写一些 三目写法到底要不要 特殊的地方的注释一定要有 /*! * jQuery JavaScript Library ...
- HTML5开发注意事项及BUG解决
1.点透Q:元素A上定位另外一个元素B,点击元素B,如果元素A有事件或链接,会触发元素A上的事件或链接,即点透A:在元素B的touchend中增加ev.preventDefault();阻止默认事件即 ...
- ORACLE数据库删除表中记录报record is locked by another user
在操作ORACLE数据库的时候,由于执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user ...
- BZOJ1816 [Cqoi2010]扑克牌
Description 你有n种牌,第i种牌的数目为ci.另外有一种特殊的 牌:joker,它的数目是m.你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1 ...
- hihocoder1187 Divisors
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...
- UVa 12505 Searching in sqrt(n)
传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...
- ppa安装php版本
如果你想安装PHP的特定版本,那么这篇文章可以帮助你.这篇文章将帮助您安装PHP 5.4和PHP 5.5 PHP 5.6,通过使用PPA在Ubuntu 15.10 LTS,14.04或12.04 LT ...
- html checkbox 全选与反选
之所以记录这个博客,是因为我完全用jquery,无法实现自己想要的结果(通过一个checkbox的选中或不选中控制其他多个checkbox状态) <input type="checkb ...
- Extreme Learning Machine(ELM)的工程哲学
Extreme Learning Machine(ELM)的工程哲学 David_Wang2015 发布于2015年5月6日 11:29 工程问题往往需要的是一定精度范围内的结果,而不是“真正的”结果 ...