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. Java高并发实战,锁的优化

    锁优化 这里的锁优化主要是指 JVM 对 synchronized 的优化. 自旋锁 互斥同步进入阻塞状态的开销都很大,应该尽量避免.在许多应用中,共享数据的锁定状态只会持续很短的一段时间.自旋锁的思 ...

  2. 洛谷 U41571 Agent2

    U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...

  3. 立面图 平面图 剖面图 CAD

    http://www.qinxue.com/88.html http://www.xsteach.com/course/2855 前后左右各个侧面的外部投影图——立面图:对建筑物各个侧面进行投影所得到 ...

  4. android菜鸟之路-事件分发机制总结(二)

    ViewGroup事件分发机制 自己定义一个LinearLayout,ImageView和Button,小二,上代码 <LinearLayout xmlns:android="http ...

  5. 模式识别之ocr---文字识别Tesseract-OCR 进行文字识别 VS2010

    近日做铸件文字识别的项目,需要识别铸件上的字符和数字,找到开源的识别库Tesseract,下面简单记录下怎么使用. 首先在项目主页http://code.google.com/p/tesseract- ...

  6. iOS开发——高级篇——iOS 项目的目录结构

    最近闲来无事去面试一下iOS开发,让我感到吃惊的,面试官竟然问怎么分目录结构,还具体问每个子目录的文件名. 目录结构确实非常重要,面试官这么问,无疑是想窥探开发经验.清晰的目录结构,可让人一眼明白相应 ...

  7. how to modify vs2017

    https://docs.microsoft.com/en-us/visualstudio/install/modify-visual-studio 直接用everything搜索vs_install ...

  8. 【HDU 2157】 How Many Ways??

    [题目链接] 点击打开链接 [算法] 设A[i][j]为走一条边,从i走到j的方案数 C[i][j]为走两条边,从i走到j的方案数,显然有 : C = A * A = A^2 C'[i][j]为走三条 ...

  9. [luogu2620]虫洞

    https://www.zybuluo.com/ysner/note/1284536 题面 给一个一维坐标系,出发点为\(0\),目标点为\(w\). 每\(1\)秒可以往后移不超过\(s\)个单位距 ...

  10. 记录利用CSS完美解决前端图片变形问题

    在头条IT学堂看到CSS完美解决前端图片变形问题的文章,就记录分享下: 一.让图片的宽度或者高度等于容器的宽度或高度,多余的裁掉,然后让图片居中: <style type="text/ ...