Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty.

Example:
Given binary tree

          1
/ \
2 3
/ \
4 5

Returns [4, 5, 3], [2], [1].

Explanation:

1. Remove the leaves [4, 5, 3] from the tree

          1
/
2

2. Remove the leaf [2] from the tree

          1

3. Remove the leaf [1] from the tree

          []

Returns [4, 5, 3], [2], [1].

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

给一个二叉树,找出它的叶节点然后删除,重复此步骤,直到二叉树为空。

The key to solve this problem is converting the problem to be finding the index of the element in the result list. Then this is a typical DFS problem on trees.

Java:

public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
helper(result, root);
return result;
} // traverse the tree bottom-up recursively
private int helper(List<List<Integer>> list, TreeNode root){
if(root==null)
return -1; int left = helper(list, root.left);
int right = helper(list, root.right);
int curr = Math.max(left, right)+1; // the first time this code is reached is when curr==0,
//since the tree is bottom-up processed.
if(list.size()<=curr){
list.add(new ArrayList<Integer>());
} list.get(curr).add(root.val); return curr;
} 

Python:

class Solution(object):
def findLeaves(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
def findLeavesHelper(node, result):
if not node:
return -1
level = 1 + max(findLeavesHelper(node.left, result), \
findLeavesHelper(node.right, result))
if len(result) < level + 1:
result.append([])
result[level].append(node.val)
return level result = []
findLeavesHelper(root, result)
return result

C++:

// Time:  O(n)
// Space: O(h) /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> result;
findLeavesHelper(root, &result);
return result;
} private:
int findLeavesHelper(TreeNode *node, vector<vector<int>> *result) {
if (node == nullptr) {
return -1;
}
const int level = 1 + max(findLeavesHelper(node->left, result),
findLeavesHelper(node->right, result));
if (result->size() < level + 1){
result->emplace_back();
}
(*result)[level].emplace_back(node->val);
return level;
}
};

   

类似题目:

[LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

[LeetCode] 310. Minimum Height Trees 最小高度树

[LeetCode] 545. Boundary of Binary Tree 二叉树的边界

All LeetCode Questions List 题目汇总

最新文章

  1. 启用CentOS6.5 64位安装时自带的MySQL
  2. UNITY更新到5后设置的动画无法播放了怎么办
  3. PHP扩展下载指导
  4. JavaScript 命名规则
  5. linux内核调优详解
  6. CSS 块状元素和内联元素
  7. Tomcat AccessLog 格式化
  8. POJ2236 Wireless Network
  9. DOS命令之----Netstat+Task以及相关使用
  10. 【有趣~】SFOJ-1711 Obey的恋爱、NYOJ-739 笨蛋难题
  11. 24种设计模式--状态模式【State Pattern】
  12. 几种常用的Java数据源解决方案
  13. 【图文教程】用“iz3d”软件将您的游戏打造为红蓝3D游戏。
  14. Shell 传递参数
  15. Maven 使用笔记
  16. ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)
  17. 小议SQL数据插入
  18. Scala路径依赖【内部类】
  19. crunch创建自己的密码字典文件
  20. 使用 scp命令免登陆

热门文章

  1. LeetCode - 24、两两交换链表中的节点
  2. SD介绍
  3. Docker创建mysql镜像
  4. oracle连接出现的坑
  5. circus 架构
  6. 用pickle保存机器学习模型
  7. C语言博客作业00--我的第一篇博客
  8. mysql 过滤分组
  9. Android中如何动态添加碎片
  10. 无人机一体化3DGIS服务平台