LeetCode——Find Largest Value in Each Tree Row
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的更多相关文章
- [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 ...
- LeetCode: Find Largest Value in Each Tree Row
BFS /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- 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 ...
- 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 ...
- 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 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 ...
- (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 ...
- 【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 ...
随机推荐
- Unpacking and repacking stock rom .img files
http://forum.xda-developers.com/galaxy-s2/general/ref-unpacking-repacking-stock-rom-img-t1081239 OP ...
- Android Handler 的使用
Android UI 操作是线程不安全的.我们只能在UI线程或者说主线程中修改UI.试想多个Thread操作同一个UI,可能引起不一致.UI 线程的主要工作是:UI界面更新显示,各个控件的交互等等.一 ...
- sql server下划线查询
select * from tablea A where A.b like '%[_]%'
- MySQL给一个字段递增赋值
https://blog.csdn.net/kriszhang/article/details/72125203 首先设置一个变量,初始值为0: set @r:=0; 1 然后更新表中对应的ID列: ...
- JSP学习(第二课)
把GET方式改为POST在地址栏上就不会显示. 发现乱码了,设置编码格式(这个必须和reg.jsp中page的charset一致): 但是注意了!我们传中文名,就会乱码: 通过get方式提交的请求无 ...
- Linux下多线程的重要知识点
线程属性: typedef struct { int detachstate; 线程的分离状态 int ...
- ObjectDetection中的一些名词中英文对照
mAP:mean Average Precision,平均精确度 recall rate:召回率 Loss Function Anchro
- linux qt下 QSqlDatabase: QMYSQL driver not loaded
出现上述问题是qt安装目录未包含mysql驱动. 解决方法如下: 1.查看系统是否存在libqsqlmysql.so find / -name libqsqlmysql.so 2.若不存在该文件则安装 ...
- R中seurat等问题学习
1.Seurat 转自:https://cloud.tencent.com/developer/article/1055892 # Initialize the Seurat object with ...
- 算法总结之动态规划(DP)
适用动态规划的特点 所解决的问题是最优化问题. 所解决的问题具有"最优子结构".可以建立一个递推关系,使得n阶段的问题,可以通过几个k<n阶段的低阶子问题的最优解来求解. 具 ...