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:

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:

          []

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

 vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> ret;
removeLeaves(ret, root);
return ret;
} int removeLeaves(vector<vector<int>> & ret, TreeNode* root) {
if (root == NULL) return ;
int d1 = removeLeaves(ret, root->left);
int d2 = removeLeaves(ret, root->right);
int lev = max(d1, d2) + ;
if (ret.size() <= lev) ret.resize(lev);
ret[lev - ].push_back(root->val);
return lev;
}

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

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

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

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

  4. LeetCode 366. Find Leaves of Binary Tree

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

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

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

  7. 【LeetCode】366. Find Leaves of Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

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

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

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

随机推荐

  1. mac 安装php7

    卸载php55 brew unlink php55 brew install homebrew/php/php70 安装成功信息 To enable PHP in Apache add the fol ...

  2. js数组倒叙输出

    第一种:是直接利用代码进行输出 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  3. 微博开放平台开发(一)获取access_token

    因为工作需要,接触到微博开放平台开发.特做此记录方便查用. 一.准备. 1.微博账号.注册很容易. 2.微博账号成为开发者. 登录微博开放平台  登录你注册的账号,然后进入管理中心完善开发者基本信息和 ...

  4. Effective Scala

    Effective Scala Marius Eriksen, Twitter Inc.marius@twitter.com (@marius)[translated by hongjiang(@ho ...

  5. 打造最高效的科研环境之Emacs插件们

    0 盲人摸象 作为初学者,迫切的需求就是直接上手Emacs并打造包含自动补全命令在内的科研环境. 和网上众多的插件安装的教程相比,我认为找到一个与自己需求匹配的Emacs配置环境来得更方便. 本例中, ...

  6. 伪静态重写模块rewrite.dll及httpd.ini文件参考下载

    伪静态重写模块rewrite.dll及httpd.ini文件参考下载 http://www.ledaokj.com/download/rewrite.rar 服务器端开启伪静态,可以查看以下文章< ...

  7. 详解.Net消息队列(MSMQ)应用

    [IT168 技术文档]MSMQ是Windows 2000.Windows XP.Windows Server 2003的一个组件,并将继续包含在Windows Vista和以后的Windows服务器 ...

  8. MSChart绘图控件中折线图和柱形图画法

    首先在前台拖入一个名为chart1的MSChart控件 //折线图 string strLegend = "Legend1"; Legend lg = new Legend(str ...

  9. pdsh使用

    原文地址:http://bbs.chinaunix.net/thread-2002080-1-1.html 当我们管理数以十计或者更多的集群系统时,往往需要在每台机器上执行同样的命令,或者拷贝同样的文 ...

  10. Adding a WebPart to a SharePoint 2013 Master Page 分类: Sharepoint 2015-07-08 01:03 7人阅读 评论(0) 收藏

    On SharePoint 2013 you can not add the Web Parts to the master page the same way of 2010. Please use ...