Question

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]

Solution

层次遍历。

Code

/**
* 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 == NULL)
return vector<int>();
queue<TreeNode*> rows, new_rows;
new_rows.push(root);
rows = new_rows;
vector<int> res;
int max_value = INT_MIN;
while (!new_rows.empty()) {
max_value = INT_MIN;
clear(new_rows);
while (!rows.empty()) {
TreeNode* tmp = rows.front();
if (tmp->val > max_value)
max_value = tmp->val;
rows.pop();
if (tmp->left != NULL)
new_rows.push(tmp->left);
if (tmp->right != NULL)
new_rows.push(tmp->right);
}
res.push_back(max_value);
rows = new_rows;
}
return res;
}
void clear(queue<TreeNode*>& q) {
queue<TreeNode*> empty;
swap(q, empty);
}
};

LeetCode——Find Largest Value in Each Tree Row的更多相关文章

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

  2. LeetCode: Find Largest Value in Each Tree Row

    BFS /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...

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

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

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

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

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

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

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

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

随机推荐

  1. CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构,通用对象请求代理体系结构)是由OMG组织制订的一种标准的面向对象应用程序体系规范

    CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构,通用对象请求代理体系结构)是由OMG组织制订的一种标准的面向对象应用程序体系规 ...

  2. Windows 下配置 php_imagick 扩展

    1.首先按装 imageimagick 可以去 http://imagemagick.org/script/binary-releases.php#windows 这里下载,看好自己的系统环境和选择好 ...

  3. 原!linux脚本统计

    #! /bin/sh first=$ first2=$ input2=$ let second=`date -d "-1 days ago ${input2}" +%Y%m%d` ...

  4. ubuntu 编辑pdf

    参考:https://www.2cto.com/kf/201710/689121.html Ubuntu下几个功能比较全面的PDF编辑工具. 1.flpsed flpsed是一个WYSIWYG的“伪” ...

  5. Linux命令(基础2)

    1.命令概要介绍: 查看目录内容:ls 切换目录命令:cd 创建与删除目录:touch(创建文件).rm(移除文件与目录).mkdir(创建目录) 拷贝与移动命令:cp(拷贝).mv(移动) 查看文件 ...

  6. 类方法classmethod 绑定方法

    绑定方法 1.凡是类中的方法和函数,都是绑定给对象使用的: 2.绑定方法都有自动传值的功能.传递进去的值,就是对象本身. 3.如果类想调用绑定方法,就必须遵循函数的参数规则,有几个参数,就必须传递几个 ...

  7. SQL Server 排名函数

    个函数进行的解释. 以下是对这4个函数的解释: RANK() 返回结果集的分区内每行的排名.行的排名是相关行之前的排名数加一. 假设两个或多个行与一个排名关联,则每一个关联行将得到同样的排名. 比如, ...

  8. 3类数据库的联动:mysql、mongodb、redis

    3类数据库的联动:mysql.mongodb.redis from pymysql import * from pymongo import * from redis import * class M ...

  9. 图解Python可以做些什么

    Python具有简单.易学.免费.开源.可移植.可扩展.可嵌入.面向对象等优点,它的面向对象甚至比java和C#.net更彻底. 作为一种通用语言,Python几乎可以用在任何领域和场合,角色几乎是无 ...

  10. mysql 下的命令

    1.查看mysql日志vim /var/log/mysqld.log