[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:
Input: [1,2,3,4,5]
1
/ \
2 3
/ \
4 5 Output: [[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:
[]
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
这道题给了我们一个二叉树,让我们返回其每层的叶节点,就像剥洋葱一样,将这个二叉树一层一层剥掉,最后一个剥掉根节点。那么题目中提示说要用DFS来做,思路是这样的,每一个节点从左子节点和右子节点分开走可以得到两个深度,由于成为叶节点的条件是左右子节点都为空,所以我们取左右子节点中较大值加1为当前节点的深度值,知道了深度值就可以将节点值加入到结果res中的正确位置了,求深度的方法我们可以参见 Maximum Depth of Binary Tree 中求最大深度的方法,参见代码如下:
解法一:
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
helper(root, res);
return res;
}
int helper(TreeNode* root, vector<vector<int>>& res) {
if (!root) return -;
int depth = + max(helper(root->left, res), helper(root->right, res));
if (depth >= res.size()) res.resize(depth + );
res[depth].push_back(root->val);
return depth;
}
};
下面这种DFS方法没有用计算深度的方法,而是使用了一层层剥离的方法,思路是遍历二叉树,找到叶节点,将其赋值为NULL,然后加入leaves数组中,这样一层层剥洋葱般的就可以得到最终结果了:
解法二:
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
while (root) {
vector<int> leaves;
root = remove(root, leaves);
res.push_back(leaves);
}
return res;
}
TreeNode* remove(TreeNode* node, vector<int>& leaves) {
if (!node) return NULL;
if (!node->left && !node->right) {
leaves.push_back(node->val);
return NULL;
}
node->left = remove(node->left, leaves);
node->right = remove(node->right, leaves);
return node;
}
};
还有一种不用建立新的递归函数的方法,就用本身来做递归,我们首先判空,然后对左右子结点分别调用递归函数,这样我们suppose左右子结点的所有叶结点已经按顺序存好到了二维数组left和right中,现在要做的就是把两者合并。但是我们现在并不知道左右子树谁的深度大,我们希望将长度短的二维数组加入到长的里面,那么就来比较下两者的长度,把长度存到结果res中,把短的存入到t中,然后遍历短的,按顺序都加入到结果res里,好在这道题没有强行要求每层的叶结点要按照从左到右的顺序存入。当左右子树的叶结点融合完成了之后,当前结点也要新开一层,直接自己组一层,加入结果res中即可,参见代码如下:
解法三:
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
if (!root) return {};
vector<vector<int>> left = findLeaves(root->left), right = findLeaves(root->right);
vector<vector<int>> res = (left.size() >= right.size()) ? left : right;
vector<vector<int>> t = (left.size() >= right.size()) ? right : left;
for (int i = ; i < t.size(); ++i) {
res[i].insert(res[i].begin(), t[i].begin(), t[i].end());
}
res.push_back({root->val});
return res;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/find-leaves-of-binary-tree/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点的更多相关文章
- [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: 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 543. Diameter of Binary Tree (二叉树的直径)
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)
题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) /** * Definition ...
- [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]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】#106. Construct Binary Tree from Inorder and Postorder Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
随机推荐
- Vertica license导入最佳实践
常用的方法,我们可以通过admintools字符图形工具来导入license, 7 -> 5 -> 选择库 -> 输入license文件全路径 -> Accept -> ...
- 学习Spring——依赖注入
前言: 又开始动笔开了“学习Spring”系列的头…… 其实一开始写“学习SpringMVC”的几篇文章是出于想系统的了解下Spring以及SpringMVC,因为平时在公司中虽然每天都在使用Spri ...
- 你真的会玩SQL吗?简单的数据修改
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- CSS3 值得称赞新特性
Html5和CSS3相信大家现在都已不陌生了吧,但CSS3哪些新特性值得我们去称赞呢? 首先还是让大家来看几张效果图,相信大家看到这些效果图,肯定会说这些效果只用CSS是如何实现的呢? 1.3D正方形 ...
- C++ map的基本操作和使用
原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...
- EC笔记:第三部分:16成对使用new和delete
我们都知道,申请的资源,使用完毕后要释放.但是这个释放动作,一定要注意. 举个例子,很多人动态分配的资源,在使用之后,往往直接调用了delete,而不管申请资源的时候用的是new还是new[]. 如下 ...
- JAVA 日期格式工具类DateUtil.java
DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...
- Maven远程仓库的认证
大部分远程仓库无须认证就可以访问,但有时处于安全方面的考虑,我们需要提供认证信息才能访问一些远程仓库.为了防止非法的仓库访问,管理员为每个仓库提供了一组用户名及密码. 这时,为了能让Maven访问仓库 ...
- iOS - GitHub干货分享(APP引导页的高度集成 - DHGuidePageHUD - ②)
距上一篇博客"APP引导页的高度集成 - DHGuidePageHUD - ①"的发布有一段时间了, 后来又在SDK中补充了一些新的内容进去但是一直没来得及跟大家分享, 今天来跟大 ...
- iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法
"UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...