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;
}
};

650. Find Leaves of Binary Tree的更多相关文章

  1. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

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

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. LeetCode 366. Find Leaves of Binary Tree

    原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...

  9. [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 ...

随机推荐

  1. Docker 0x05: Dockerfile制作镜像

    目录 Dockerfile制作镜像 一句话什么是dockerfile dockerfile脚本指令 小结 Dockerfile制作镜像 dockerfile 可以是制作自己镜像的脚本文件,按照这个脚本 ...

  2. css overflow失效的原因

    声明 转载自https://my.oschina.net/xuqianwen/blog/540587 项目中常常有同学遇到这样的问题,现象是给元素设置了overflow:hidden,但超出容器的部分 ...

  3. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  4. [转]etcdctl v2 v3 使用指南

    原文:https://blog.csdn.net/kozazyh/article/details/79586530 ------------------------------------------ ...

  5. P1850 换教室[dp+期望]

    流下了不会概率的眼泪,由于不会概率,转移少写了点东西... 这个dp很简单,就是一个普通的线性dp加点期望.(刚开始写这道题时信笔写下) \(dp[0/1][i][j]\)表示到第\(i\)个时间段时 ...

  6. beta冲刺(3/7)

    作业格式 课程名称:软件工程1916|W(福州大学) 作业要求:项目beta冲刺(团队) 团队名称: 那周余嘉熊掌将得队 作业目标:beta(3/7) 队员学号 队员姓名 博客地址 备注 221600 ...

  7. discuz! X3.4特殊字符乱码解决方案

    Discuz! X3.4升级后,帖子内容使用Unicode编码会出现直接显示源码问题 打开:source\function\function_core.php $string = str_replac ...

  8. 运维常用shell脚本之日志清理

    1.创建一个日志清理脚本 #/bin/bash for i in `find /root/.pm2/logs -name "*.log"` do cat /dev/null > ...

  9. GoogleUpdate计划任务提权

    1.默认低权限无法写入: 2.配置低权限可操控:icacls Update /grant everyone:f 3.下载可执行程序:certutil -urlcache -split -f http: ...

  10. continue语句:编程把100-300之间的能被25整除的数输出

    #include<stdio.h>void main(){ int n; for(n=100;n<=300;n++) { if(n%25!=0) continue; printf(& ...