实质就是求每个节点的最大深度。用一个hash表记录,最后输出。

class Solution {
public:unordered_map<TreeNode *,int> hash; // record the level from bottom
    vector<vector<int>> findLeaves(TreeNode* root) {vector<vector<int>> res;dfs(root);//for (auto x:hash) cout << x.first->val << ' ' << x.second << endl;for (int i=1;i<=hash[root];++i){vector<int> tmp;for (auto x:hash){if (x.second==i)tmp.push_back(x.first->val);}res.push_back(tmp);}return res;}int dfs(TreeNode *root){if (root==NULL) return 0;int depth=max(dfs(root->left),dfs(root->right))+1;hash[root] = depth;return depth;}
};

其实可以不用hash表,每次深度比vector.size()大的时候新建一个vector,这样节省了空间。

类似的方法在别的题里也有应用。

class Solution {
public:vector<vector<int>> res;vector<vector<int>> findLeaves(TreeNode* root) {dfs(root);return res;}int dfs(TreeNode *root){if (root==NULL) return 0;int depth=max(dfs(root->left),dfs(root->right))+1;if (depth>res.size()) res.push_back(vector<int>());res[depth-1].push_back(root->val);return depth;}
};

转载于:https://www.cnblogs.com/hankunyan/p/9583602.html

LeetCode 366. Find Leaves of Binary Tree相关推荐

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. LeetCode: 104. Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  3. leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

    题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetcode ...

  4. LeetCode 662. Maximum Width of Binary Tree

    原题链接在这里:https://leetcode.com/problems/maximum-width-of-binary-tree/ 题目: Given a binary tree, write a ...

  5. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  6. LeetCode 297 Serialize and Deserialize Binary Tree

    题目描述 Serialization is the process of converting a data structure or object into a sequence of bits s ...

  7. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  8. LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy

    要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离) Given a binary tree, find its maximum depth.The maxi ...

  9. leetcode 606. Construct String from Binary Tree | 606. 根据二叉树创建字符串

    题目 https://leetcode-cn.com/problems/construct-string-from-binary-tree/ 题解 当右子树非空时,不管有没有左子树,一定要用()将左子 ...

最新文章

  1. php实现调查结果百分比显示,Php文本游戏试图根据百分比做出结果
  2. 第一阶段SCRUM冲刺 03
  3. 网页标准HTML5标准较量正酣
  4. mysql 按月统计代码_SQL对数据进行按月统计或对数据进行按星期统计的实例代码...
  5. jpane1_Java—面板组件(Jpanel)1
  6. WinUI 3 Preview 3 发布了,再一次试试它的性能
  7. 高速公路、铁路交通的常识
  8. c3p0连接池配置连接不上mysql_数据库连接池之c3p0的配置 + 问题解决方案
  9. c语言窗体关机程序代码,c语言 关机程序代码
  10. Apple ID Your Account Cannot Be Created at This Time
  11. 【华人学者风采】周明 微软亚洲研究院
  12. 打卡day01 python基础—常用数据类型
  13. JS校验统一社会信用代码的真实性
  14. 【JS30-Wes Bos】实时显示的时钟网页 02
  15. 防关联浏览器有哪些浏览器(浏览器防关联软件分析)
  16. Java web网站访问量的计数
  17. 20220614 笔记
  18. Verilog数字系统设计——8位数字比较器
  19. 詹姆斯高斯林_詹姆斯·高斯林(James Gosling)加入RCDb顾问委员会
  20. Qt开发经验小技巧241-245

热门文章

  1. css 菜单栏悬停_在CSS中构建悬停菜单
  2. 平面设计师和ui设计师_平面设计师为什么要享受所有乐趣?
  3. 「Vueconf」探索 Vue3 中 的 JSX
  4. HDU 1253 胜利大逃亡 题解
  5. php 自带 web server 如何重写 rewrite .htaccess
  6. Lind.DDD.Manager里的3,7,15,31,63,127,255,511,1023,2047
  7. mysql添加普通用户用于管理单一数据库
  8. php实现pdf文件的生成与下载
  9. linux系统远程教程,Linux下实现远程协助
  10. [学习笔记]状压dp