[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 9
Output: [1, 3, 9]
这道题让我们找二叉树每行的最大的结点值,那么实际上最直接的方法就是用层序遍历,然后在每一层中找到最大值,加入结果res中即可,参见代码如下:
解法一:
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if (!root) return {};
vector<int> res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int n = q.size(), mx = INT_MIN;
for (int i = ; i < n; ++i) {
TreeNode *t = q.front(); q.pop();
mx = max(mx, t->val);
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
res.push_back(mx);
}
return res;
}
};
如果我们想用迭代的方法来解,可以用先序遍历,这样的话就需要维护一个深度变量depth,来记录当前结点的深度,如果当前深度大于结果res的长度,说明这个新一层,我们将当前结点值加入结果res中,如果不大于res的长度的话,我们用当前结点值和结果res中对应深度的那个结点值相比较,取较大值赋给结果res中的对应深度位置,参见代码如下:
解法二:
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if (!root) return {};
vector<int> res;
helper(root, , res);
return res;
}
void helper(TreeNode* root, int depth, vector<int>& res) {
if (depth > res.size()) res.push_back(root->val);
else res[depth - ] = max(res[depth - ], root->val);
if (root->left) helper(root->left, depth + , res);
if (root->right) helper(root->right, depth + , res);
}
};
参考资料:
https://discuss.leetcode.com/topic/79241/simple-and-easy-understand-c-dfs-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值的更多相关文章
- 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 ...
- 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 ...
随机推荐
- 关闭系统邮件提醒:you hava a new mail(转)
有时候,在输入某些触及到系统安全或者内核方面的命令都会提醒你: You have new mail in /var/spool/mail/root 只需要在root 用户下,不设置邮件检测即可! #e ...
- 获取服务器时间js代码
function getSevertime(){ var xmlHttp = new XMLHttpRequest(); if( !xmlHttp ){ xmlHttp = new ActiveXOb ...
- Oracle Orion tool check io(ORACLE Orion 工具查看以及校验IO)
文档主要来自oracle官方文档performance 8.3章节 Oracle数据库提供了Orion,一种 I/O校准工具.Orion是预测Oracle数据库性能的工具,无需安装Oracle或创建数 ...
- shell随机生成身份证,姓名,电话,日期,分数,等级和insert语句
#!/bin/bash#生成随机身份证号,性别,年龄,电话,姓名,日期,分数和对应等级,并生成insert语句#作者AiYS,2018-02-06,转载请注明http://www.cnblogs.co ...
- hibernate框架学习笔记9:多对多关系案例
员工与角色案例: 一个员工可以是多种角色(总监,经理),一种角色可以是多个员工(保洁) 这里发现无法使用外键表达关系,多对多总是创建第三张表来维护关系 这张表至少两列,都是外键,分别引用两张表的主键 ...
- python 二叉堆
BinaryHeap() 创建一个新的,空的二叉堆. insert(k) 向堆添加一个新项. findMin() 返回具有最小键值的项,并将项留在堆中. delMin() 返回具有最小键值的项,从堆中 ...
- SQL之Left Join 关联条件的探讨
在测试工作中,有时需要测试数据库数据经过sql计算后的结果是否满足某一功能查询得到的返回值. 针对某些需要功能需要联查多张表,此时 关联 的作用就异常重要了,而针对多表关联,其中 关联条件的重要性不言 ...
- JQ.ajax 各种参数及属性设置 ( 转载 )
$.ajax({ type: "post", url: url, dataType:'html', success: function(da ...
- sublimeText3 中配置sass环境,并将编译后文件保存到指定文件夹
sass基于ruby引擎,所以安装时ass.compass之前需要安装ruby.具体的链接应该是(http://rubyinstaller.org/downloads).下载并安装相应的版本,勾选第二 ...
- jmeter入门(02)测试报告各项指标含义
一.名词定义(时间单位ms) 1.聚合报告 Sample:本次测试场景共运行多少个请求: Average:平均响应时间: Median:统计意义上的响应时间中值: 90% line:所有线程中90%的 ...