在二叉树的每一行中找到最大的值。
示例:
输入:
          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. MRUnit测试

    新建一个专门的测试类,代码如下: wordcount的map函数输入string line,      输出<单词 , 1> 右键-> run as junit 出错了,因为输出不是 ...

  2. ermissions on /usr/local/mongodb/conf/keyFile are too open

    ermissions on /usr/local/mongodb/conf/keyFile are too open > rs.initiate(cfg); { "ok" : ...

  3. hadoop yarn namenode datanoe 启动异常问题解决 分析日志

    cat logs/hadoop-root-datanode-hadoop1.log ********************************************************** ...

  4. mybatis中GenericTokenParser的用法

    代码示例 import org.apache.ibatis.parsing.GenericTokenParser;import org.apache.ibatis.parsing.TokenHandl ...

  5. ⭐驱动之module_init/module_exit与系统启动关系

    在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...

  6. YTU 2420: C语言习题 不等长字符串排序

    2420: C语言习题 不等长字符串排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 460  解决: 239 题目描述 在主函数中输入n(n<=10)个不等长的字符串.用另一函 ...

  7. FileReader、 FileWriter、readLine()和newLine()、LineNumberReader(二十一)

    1.字符流FileReader * 1.字符流是什么 * 字符流是可以直接读写字符的IO流 * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. ...

  8. I.MX6 Android /data 目录内容

    /**************************************************************************** * I.MX6 Android /data ...

  9. [SoapUI] Jenkins 配置

    cd %WORKSPACE% cmd /c call "%SOAPUI_PRO_HOME%\bin\testrunner.bat" -a -j -PprojectPath=&quo ...

  10. 「网络流24题」「LuoguP4016」 负载平衡问题

    Description GGG 公司有 nnn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 nnn 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. ...