lc 515 Find Largest Value in Each Tree Row


515 Find Largest Value in Each Tree Row

You need to find the largest value in each row of a binary tree.

Example:

Input:

      1
/ \
3 2
/ \ \
5 3 9

Output: [1, 3, 9]

BFS Accepted

相比之前的几道用到BFS算法的题目,这题相对来说会繁琐一些,需要用变量记录每一个节点所在的层数l,当前比较大小的层数m,重要的是厘清思路,其实也挺简单的,就是容易脑子搭线,陷入僵局。

/**
* 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<int> largestValues(TreeNode* root) {
if (!root) return {};
queue<TreeNode*> q;
queue<int> level;
vector<int> ans;
q.push(root);
level.push(0);
int m = -1;
while(!q.empty()) {
TreeNode* temp = q.front();
q.pop();
int l = level.front();
level.pop();
if (temp->left) {
q.push(temp->left);
level.push(l+1);
}
if (temp->right) {
q.push(temp->right);
level.push(l+1);
}
if (l > m) {
m = l;
ans.push_back(temp->val);
} else {
ans[l] = max(ans[l], temp->val);
}
}
return ans;
}
};

LN : leetcode 515 Find Largest Value in Each Tree Row的更多相关文章

  1. (BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  2. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

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

  3. 515. Find Largest Value in Each Tree Row查找一行中的最大值

    [抄题]: You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...

  4. 515 Find Largest Value in Each Tree Row 在每个树行中找最大值

    在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [ ...

  5. 【leetcode】Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  6. 515. Find Largest Value in Each Tree Row

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  7. LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...

  8. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  9. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

随机推荐

  1. JSP国际化设置

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/internationalization.html: 国际化(i18n):这意味着可以使网站根据访问者的语 ...

  2. apple applessd.sys error

    http://bbs.feng.com/read-htm-tid-10242622.html

  3. Property 'sqlMapClient' is required

    继承SqlMapClientDaoSupport类的类里面添加如下代码 @Resource(name = "sqlMapClient") private SqlMapClient ...

  4. Base Conversion In PHP and javascript

    http://www.exploringbinary.com/base-conversion-in-php-using-built-in-functions/ http://www.binarycon ...

  5. iOS开发项目实战——Swift实现图片轮播与浏览

    近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...

  6. Android API Guides –System Permissions

    系统权限 声明: 本文由Gordon翻译 公布于www.dlvoice.com 欢迎转载,但请保留此声明 原文地址:http://developer.android.com/guide/topics/ ...

  7. Enterprise Library 5.0 学习笔记

    近期了解了微软提供的企业开发框架Enterprise Library,眼下最新版本号是6.0,可是不支持FW3.5.所以就学习了5.0的版本号,EL5.0支持FW3.5和4.0,官网下载地址是:htt ...

  8. 通过uri呼起本地app

    1.在Android本地app清单文件里配置 <activity android:name="com.mdj.ui.WelcomeActivity" android:scre ...

  9. linux启动基本流程

    linux启动序列 1.CPU初始化    CPU自身初始化.从某个固定位置(0xfffffff0)取指令并运行,该指令为跳转指令.跳转到BIOS代码的首部. 2.装载BIOS    BIOS被固化在 ...

  10. ZOJ3659 Conquer a New Region 并查集

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...