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]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的思路:

1. 用广搜,但是这道题在leetcode上是深搜,我感觉用广搜好解一点,于是选择广搜

2. 因为要记录每一层值,所以在每次进入while循环,先看里面有几个元素,就是每一层的结点个数

3. 然后再在每层中找最大值即可

/*** 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:queue<TreeNode*> q; //存放值vector<int> maxValue;vector<int> largestValues(TreeNode* root) {if(root==NULL) return maxValue;q.push(root);while(!q.empty()){int size = q.size();int maxnV = INT_MIN;for(int i = 0; i < size; i++){TreeNode* front = q.front();int v = front->val; //队头的值maxnV = max(maxnV, v);q.pop();if(front->left) q.push(front->left);if(front->right) q.push(front->right);}maxValue.push_back(maxnV);                 }return maxValue;}
};

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

  1. LeetCode 515. Find Largest Value in Each Tree Row

    515. Find Largest Value in Each Tree Row You need to find the largest value in each row of a binary ...

  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. SAP MM 如何查询物料凭证号是由哪个IDoc Post后产生的?
  2. ORU-10027: buffer overflow, limit of 10000 bytes
  3. 年终凡尔赛,都是别人家的公司...
  4. python装饰器是什么意思_这个python 装饰器代码是什么意思?
  5. 交换算法经常使用的两个数的值
  6. 父子组建传值_浅谈Vue父子组件和非父子组件传值问题
  7. node ajax validator,使用validator.js对字符串数据进行验证
  8. python字典求平均值_Python - 字典中各个键的每个值的均值
  9. 要害怕做事的s9t9
  10. Cell重磅综述:关于人类转录因子,你想知道的都在这
  11. CWNP认证和Cisco无线认证区别
  12. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(一)
  13. 《剑指offer》-连续子数组的最大和
  14. android获取电池是否充电,Android 判断电池是否为充电状态的方法
  15. 最经济方案 谈P2P电影服务器
  16. 2018年,免费、无水印录屏软件有哪些?
  17. snmp trap配置
  18. 黑苹果10.15.7安装comfast永存,CF-811AC驱动方法
  19. 'grunt' 不是内部或外部命令,也不是可运行的程序 或批处理文件
  20. MVC已过时,MOVE时代来临?

热门文章

  1. 【玩转cocos2d-x之九】动作类CCAction
  2. 用Python实现归并排序
  3. 通过putty和 winscp操作aws机器笔记
  4. QUIC 是如何解决TCP 性能瓶颈的?
  5. 怎样的代码算是好代码?
  6. 《微服务架构设计模式》总结,文末送书
  7. 数据结构--图(Graph)详解(一)
  8. 新一代音视频技术架构驱动未来多媒体创新
  9. 未来经济 数字优先 | 大数据专场
  10. GoLang:你真的了解 HTTPS 吗?