LeetCode:Find Largest Value in Each Tree Row
来源:互联网 发布:四旋翼飞控编程 编辑:程序博客网 时间:2024/06/07 01:15
题目链接:https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/
主要思路:使用宽度优先搜索算法,遍历完一层的结点,比较得出该行的最大值,然后继续遍历下一层。
代码如下:
vector<int> largestValues(TreeNode* root) { vector<TreeNode*> now_row; vector<int> result; if (root != NULL) { now_row.push_back(root); result.push_back(root->val); while(!now_row.empty()) { vector<TreeNode*> next_row; vector<int> row_val; while(!now_row.empty()) { TreeNode *temp = now_row.back(); now_row.pop_back(); if (temp->left != NULL) { row_val.push_back(temp->left->val); next_row.push_back(temp->left); } if (temp->right != NULL) { row_val.push_back(temp->right->val); next_row.push_back(temp->right); } } int max; if (!row_val.empty()) { max = row_val[0]; for(int i = 1; i < row_val.size(); i++) { if (row_val[i] > max) { max = row_val[i]; } } result.push_back(max); } now_row.clear(); for(int i = 0; i < next_row.size(); i++) { now_row.push_back(next_row[i]); } } } return result; }
阅读全文
0 0
- LeetCode 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- 【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
- [leetcode 515]Find Largest Value in Each Tree Row
- LeetCode笔记:515. Find Largest Value in Each Tree Row
- [leetcode] 515. Find Largest Value in Each Tree Row
- 【LeetCode】515. Find Largest Value in Each Tree Row
- LeetCode;515. Find Largest Value in Each Tree Row
- LeetCode #515: Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- leetcode 515 Find Largest Value in Each Tree Row C++
- leetcode 515. Find Largest Value in Each Tree Row
- 【LeetCode】Find Largest Value in Each Tree Row 解题报告
- leetcode-515. Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- Leetcode 515 Find Largest Value in Each Tree Row
- 可靠性特征的数学表达式及其关系
- Matlab和C/C++混编调试
- 简单指南针icon(不像icon的icon..)
- Qt与MSVC中文乱码问题的解决方案
- 三、JS【JavaScript弱类型的脚本语言】03
- LeetCode:Find Largest Value in Each Tree Row
- python shelve模块的用法
- hdu 4498 自适应simpson
- WebRTC视频帧渲染前处理——等比例填充显示窗口
- 滴滴出行之地下迷宫
- 移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
- 第四章 对象与类
- 学习记录7(17/10/02 于隆昌)
- HTML 注释 水平线