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

Output: [1, 3, 9]

题目大意:找二叉树每一层最大的数~然后返回一个数组表示每一层的最大的数~
分析:先用广度优先搜索对二叉树进行层序遍历,每一层设立maxn保存每一层的最大值,然后在每一层遍历完毕之后将maxn的值放入result数组中~

class Solution {
public:vector<int> largestValues(TreeNode* root) {vector<int> result;queue<TreeNode *> q;if (root == NULL) return result;q.push(root);TreeNode *temp;while (!q.empty()) {int size = q.size();int maxn = INT_MIN;while (size--) {temp = q.front();q.pop();maxn = max(maxn, temp->val);if (temp->left != NULL) q.push(temp->left);if (temp->right != NULL) q.push(temp->right);}result.push_back(maxn);}return result;}
};

LeetCode 515. Find Largest Value in Each Tree Row相关推荐

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

  2. 【Breadth-first Search 】515. Find Largest Value in Each Tree Row

    输入:一颗二叉树 输出:这棵树每一层的最大值. 分析:和513 题目一样,处理层次问题,使用BFS最直观.使用和513一样的模板,只是记录下该层最大值即可. 分析2:用DFS处理层次遍历的问题,需要把 ...

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

  4. [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. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  6. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  7. Leetcode 742. Closest Leaf in a Binary Tree

    Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...

  8. LeetCode 215. Kth Largest Element in an Array--数字第K大的元素--最大堆或优先队列--C++,Python解法

    题目地址:Kth Largest Element in an Array - LeetCode Find the kth largest element in an unsorted array. N ...

  9. LeetCode hard 84. Largest Rectangle in Histogram--python,java 15行,c++ 15行 解法

    题目地址: Given n non-negative integers representing the histogram's bar height where the width of each ...

最新文章

  1. 独家 | 将时间信息编码用于机器学习模型的三种编码时间信息作为特征的三种方法...
  2. Docker镜像制作规范
  3. QT各版本的源码下载地址
  4. 【Ubuntu】 Ubuntu18.04修改主机名
  5. HDU 1568 Fibonacci
  6. JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;...
  7. 最新!清华大学:元宇宙发展研究报告2.0版发布!205页PPT
  8. 你们都在吐槽的华为“浴霸”被这三家拿去用了...
  9. win10强制关闭飞行模式_Win10一键关闭自动更新
  10. HBase的rowKey设计技巧
  11. 常见后端数据存储问题解决方案
  12. 检查网络是否畅通的四个步骤
  13. TestCenter测试管理工具功能详解十六(U)
  14. Xcode9 通过无线链接调试,亲测成功
  15. 四大新品齐发:荣耀打造无缝智慧全场景生态
  16. 金三银四,我为面试所准备的面试题,不看要遭老罪喽
  17. PS导出灰度图到Unity内并生成地形
  18. 微信小程序-一个跑步app
  19. 解锁scott用户,以及查看oracle中scott用户所拥有的四张表。
  20. 面向深度学习系统的对抗样本攻击与防御

热门文章

  1. 05mycat父子表
  2. 开发一款即时通讯App,从这几步开始
  3. WannaCry感染文件恢复方法,企业再也不用愁了!
  4. 远程桌面管理工具比较(转)
  5. Python核心编程答案(自整理)
  6. Android:Activity(三):启动其他Activity并返回结果
  7. SolarWinds 软件出现3个新的严重漏洞
  8. 超燃!奇安信首度对外公开内部网络攻防演习纪实片
  9. OC CoreData简单使用
  10. CentOS 7.5安装部署Jewel版本Ceph集群