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. FIR Matlab DSP

  2. flask_sqlaichemy的json字段

    https://segmentfault.com/q/1010000009304667/a-1020000009404847

  3. Ubuntu安装Python3 和卸载

    Python2中文的解决 在py文件第一行添加 #coding=utf-8 1 规范的应该这么写 #-*- coding:utf-8 -*- 1 安装python 系统默认安装Python2 安装Py ...

  4. CentOS yum 安装node.js

    第一步: curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash - 第二步: sudo yum -y i ...

  5. IPFS搭建&集群

    下载go-ipfs wget https://github.com/ipfs/go-ipfs/releases/download/v0.4.17/go-ipfs_v0.4.17_linux-amd64 ...

  6. HDU1087:Super Jumping! Jumping! Jumping!(简单dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1087 水题,可是我却因为dp数组的初始化造成了多遍wa,这题就是求上升序列的最大和. 转移方程: 首先要对 ...

  7. 细说PHP的FPM

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++基础概念++ C ...

  8. SQLAlchemy-Utils,提供choice功能

    SQLAlchemy操作数据库建表时,无法像Django一样提供choice方法,我们开头导入SQLAlchemy-Utils来为我们提供这个功能 pip3 install sqlalchemy-ut ...

  9. 在Windows上安装Elasticsearch 5.x

    在Windows上安装Elasticsearch 5.x 自己想学习Elasticsearch,但是又不懂Linux,按照同事给的Linux安装教程,也是搞不明白,于是想先在Windows上安装一下入 ...

  10. PKU 1204 Word Puzzles(AC自动机)

    题目大意:原题链接 给定一个字符串矩阵和待查找的单词,可以朝8个不同的方向查找,输出待查找单词第一个字母在矩阵中出现的位置和该单词被查到的方向. A~H代表8个不同的方向,A代表正北方向,其他依次以4 ...