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 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相关推荐
- 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 / \ ...
- 【Breadth-first Search 】515. Find Largest Value in Each Tree Row
输入:一颗二叉树 输出:这棵树每一层的最大值. 分析:和513 题目一样,处理层次问题,使用BFS最直观.使用和513一样的模板,只是记录下该层最大值即可. 分析2:用DFS处理层次遍历的问题,需要把 ...
- [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 ...
- [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 ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- Leetcode 742. Closest Leaf in a Binary Tree
Leetcode 742. Closest Leaf in a Binary Tree Approach #1: Convert to Graph 把树转换为以k节点为中心的图,从k节点开始层序遍历, ...
- 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 ...
- 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 ...
最新文章
- 独家 | 将时间信息编码用于机器学习模型的三种编码时间信息作为特征的三种方法...
- Docker镜像制作规范
- QT各版本的源码下载地址
- 【Ubuntu】 Ubuntu18.04修改主机名
- HDU 1568 Fibonacci
- JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;...
- 最新!清华大学:元宇宙发展研究报告2.0版发布!205页PPT
- 你们都在吐槽的华为“浴霸”被这三家拿去用了...
- win10强制关闭飞行模式_Win10一键关闭自动更新
- HBase的rowKey设计技巧
- 常见后端数据存储问题解决方案
- 检查网络是否畅通的四个步骤
- TestCenter测试管理工具功能详解十六(U)
- Xcode9 通过无线链接调试,亲测成功
- 四大新品齐发:荣耀打造无缝智慧全场景生态
- 金三银四,我为面试所准备的面试题,不看要遭老罪喽
- PS导出灰度图到Unity内并生成地形
- 微信小程序-一个跑步app
- 解锁scott用户,以及查看oracle中scott用户所拥有的四张表。
- 面向深度学习系统的对抗样本攻击与防御