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

详见:https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/

C++:

/**
* 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 {};
}
vector<int> res;
queue<TreeNode*> que;
que.push(root);
int cur=1;
int mx=INT_MIN;
while(!que.empty())
{
root=que.front();
que.pop();
--cur;
if(root->val>mx)
{
mx=root->val;
}
if(root->left)
{
que.push(root->left);
}
if(root->right)
{
que.push(root->right);
}
if(cur==0)
{
cur=que.size();
res.push_back(mx);
mx=INT_MIN;
}
}
return res;
}
};

515 Find Largest Value in Each Tree Row 在每个树行中找最大值的更多相关文章

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

    您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] class Solution { public: vector<i ...

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

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

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

  4. Java实现 LeetCode 515 在每个树行中找最大值

    515. 在每个树行中找最大值 您需要在二叉树的每一行中找到最大的值. 示例: 输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [1, 3, 9] /** * Definition for ...

  5. [Swift]LeetCode515. 在每个树行中找最大值 | 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. Leetcode 515. 在每个树行中找最大值

    题目链接 https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/ 题目描述 您需要在二叉树的 ...

  7. LN : leetcode 515 Find Largest Value in Each Tree Row

    lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...

  8. (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 ...

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

随机推荐

  1. queue — A synchronized queue class

    https://docs.python.org/3.6/library/queue.html https://github.com/python/cpython/blob/3.6/Lib/queue. ...

  2. PR修改例子

    DATA: lt_items_old    LIKE TABLE OF bapiebanv   WITH HEADER LINE.   DATA: lt_items_new    LIKE TABLE ...

  3. Linux搭建lnmp环境

    在CentOS 6上使用yum安装lnmp服务,原文链接http://www.qiansw.com/yum-lnmp.html

  4. ref 与 $refs 如何关联

    先问大家一个简单的问题: 还有人记得 jquery 里面的 data 方法是如何让 DOM 节点绑定对应的数据对象的吗 有时候我们做节点关联设计的思路其实有一点类似,但是在 vue 里面多了很多概念, ...

  5. bzoj4105: [Thu Summer Camp 2015]平方运算

    填坑 我不知道怎么算的,但是所有环的LCM数不会超过60 然后用线段树维护这个东西,每个节点记录子树内的循环节 没到循环节的暴力枚举 复杂度是nlogn再乘以循环节长度 #include<cst ...

  6. YTU 2456: 评委打分

    2456: 评委打分 时间限制: 1 Sec  内存限制: 128 MB 提交: 283  解决: 52 题目描述  一个歌唱比赛,比赛每次会从观众中随即抽取几名观众给分(观众至少有5个,分数为0~1 ...

  7. 鼠标滑过TAB选项卡切换demo 可拓展

    <html> <head> <script type="text/javascript"> <!-- function ShowTabs( ...

  8. 在织梦dedecms中实现“文章标题-栏目名称-网站名”导航

    本文介绍了在dedecms中,实现文章标题-栏目名称-网站名 导航的方法,有需要的朋友参考下. 在dedecms中实现“文章标题-栏目名称-网站名”导航的方法.   第一种: 在/include/in ...

  9. kafka条件查询excel拼接

    1 SELECT COUNT(*) FROM wiseweb_crawler_metasearch_page20171214 WHERE (content like '%内蒙古%'or content ...

  10. holiday题解

    题目描述如下: 经过几个月辛勤的工作,FJ 决定让奶牛放假.假期可以在 1…N 天内任意选择一段(需要连续),每一天都有一个享受指数 W.但是奶牛的要求非常苛刻,假期不能短于 P 天,否则奶牛不能得到 ...