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 找树每行最大的结点值的更多相关文章

  1. 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 ...

  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. KVM之八:快照创建、恢复与删除

    kvm虚拟机默认使用raw格式的镜像格式,性能最好,速度最快,它的缺点就是不支持一些新的功能,如支持镜像,zlib磁盘压缩,AES加密等.要使用镜像功能,磁盘格式必须为qcow2.下面开始kvm虚拟机 ...

  2. 网站加速与Linux服务器防护

    网站加速方面 1. Nginx 配置 gzip 压缩 开启nginx gzip压缩后,网页.css.js等静态资源的大小会大大的减少,从而可以节约大量的带宽,提高传输效率,给用户快的体验.虽然会消耗c ...

  3. 云服务器Windows Server2012 配置http服务器(又称Web服务器,IIS)

    出错:无法打开运行空间池.服务器管理器WinRM插件可能已损坏或丢失. 解决方法: http://shiyousan.com/post/636308065767125916 第一步是开启WinRM服务 ...

  4. 掌握SQLServer锁的相关概念

    一.为什么要引入锁 当多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: ◆丢失更新 A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 ◆脏读 ...

  5. alpha-咸鱼冲刺day1

    一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花!) 四,问题 ...

  6. xcode进行代码覆盖率测试

    去年写的文章,搬到cnblog 本文所述的方法只对xcode5做过测试,xcode6是否可行尚未可知. 配置编译选项 首先请参考苹果官方的文档Configuring Xcode for Code Co ...

  7. iOS开发-添加圆角效果高效实现

    圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...

  8. day-6 机器学习概念及应用

    学习玩Python基础语法,今天开始进行机器学习,首先了解下机器学习和深度学习的一些基本概念和术语: 1.  机器学习概念及应用 2.  深度学习概念及应用 3.  机器学习基本术语及举例 4.  机 ...

  9. MSIL实用指南-生成接口

    本篇讲解怎么样生成接口,即interface. 一.创建类型创建一个接口类型依旧用ModuleBuilder的DefineType方法,但是它的第二个参数必须要有TypeAttributes.Inte ...

  10. java的<<左移,>>右移,>>>无符号右移

    >>右移 右移,道在二进制中,假设用一个32位的Int表示一个64,那么高位就都是0,所以当我们把整个二进制数右移,如0100000 >> 2 = 0001000,可以看到右移 ...