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. 如何取option自定义属性?

    1.SELECT代码: <select name="zcdq" id="zcdq" class="easyui-validatebox" ...

  2. 挂载xfs磁盘

    # 磁盘初始化 [root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain ...

  3. Apache2.4部署python3.6+django2.0项目

    一.安装apache Apache是非常有名的web服务器软件,如果想让我们web项目运行几乎离不开它. Apache官方网站:http://httpd.apache.org/ 根据自己的环境,选择相 ...

  4. mac配置python自然语言处理环境

    一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...

  5. 【我的Android进阶之旅】快速创建和根据不同的版本类型(Dev、Beta、Release)发布Android 开发库到Maven私服

    前言 由于项目越来越多,有很多公共的代码都可以抽取出一个开发库出来传到公司搭建好的Maven私服,以供大家使用. 之前搭建的Maven仓库只有Release和Snapshot两个仓库,最近由于开发库有 ...

  6. Zabbix server(离线版)安装手册

    由于zabbix server需要依赖MySQL及PHP的相关依赖,因此需要先安装好MySQL及PHP的相关依赖后方可安装zabbixserver. 安装MySQL 目录mysql下的rpm 1.新建 ...

  7. 使用tensorflow 构建rnn网络

    使用tensorflow实现了简单的rnn网络用来学习加法运算. tensorflow 版本:1.1 import tensorflow as tf from tensorflow.contrib i ...

  8. cpu-》内存-》磁盘

    cpu相当于计算机大脑负责计算以及发送执行命令:内存相当于人的记忆是临时存储:磁盘相当于笔记本,负责永久存储数据: 当系统需要调用硬盘当中的数据时,会将硬盘数据读入内存供cpu进行处理.cpu只会读取 ...

  9. Python-argparse-命令行与参数解析

    import argparse import numpy as np import cv2 import os import numpy.random as npr from dface.core.u ...

  10. 无线路由和无线AP的区别

    一.答疑解惑 1.什么是无线AP? AP:Access Point 接入点 无线AP:无线接入点是一个无线网络的接入点,俗称“热点”.主要有路由交换接入一体设备和纯接入点设备,一体设备执行接入和路由工 ...