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. Mongodb系列文章

    http://blog.csdn.net/congcong68 学习MongoDB 六: MongoDB查询(游标操作.游标信息)(三) Mongodb官方C#驱动示例 MongoDB Driver ...

  2. Network Security Services If you want to add support for SSL, S/MIME, or other Internet security standards to your application, you can use Network Security Services (NSS) to implement all your securi

    Network Security Services | MDN https://developer.mozilla.org/zh-CN/docs/NSS 网络安全服务 (NSS) 是一组旨在支持支持安 ...

  3. Python 线程(threading)

    Python 的thread模块是比较底层的模块,Python的threading模块是对thread做了一些包装,可以更加方便的 被使用; 1. 使用threading 模块 # 示例一: 单线程执 ...

  4. 解决 Invalid signature file digest for Manifest 问题

    idea打包的jar文件在spark执行是报错: Invalid signature file digest for Manifest 通过以下命令解决: zip -d myjob.jar META- ...

  5. windows通过ssh连接虚拟机中的ubuntu步骤

    linux端开启ssh服务 1.安装openssh-server包 sudo apt-get install openssh-server 2.启动ssh server sudo /etc/init. ...

  6. MySQL行(记录)的详细操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  7. Mybatis框架学习总结-Mybatis框架搭建和使用

    Mybatis介绍 Mybatis是一个支持普通SQL查询,存储过程,和高级映射的优秀持久层框架.Mybatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.Mybatis可以使 ...

  8. Python 调用 Shell脚本的方法

    Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...

  9. 001-hive是什么

    一.基本概念 官网含义:https://cwiki.apache.org/confluence/display/Hive/Home The Apache Hive™ data warehouse so ...

  10. node.js---sails项目开发

    http://sailsdoc.swift.ren/ 这里有 sails中文文档 node.js---sails项目开发(1)安装,启动sails node.js---sails项目开发(2)安装测试 ...