515. Find Largest Value in Each Tree Row
来源:互联网 发布:北大青鸟java培训费用 编辑:程序博客网 时间:2024/06/07 11:24
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]
此题容易想到使用GFS方法遍历,使用两层while循环,第一层用来判断树的当前层是否有节点,第二层循环每次将队列中的也就是当前层的树节点找到最大值存入vector,并且将下一层的节点推入队列中,直到树的下一层没有节点退出循环。
class Solution {public: vector<int> largestValues(TreeNode* root) { vector<int> res; queue<TreeNode*> q; if (root) q.push(root); int curr = q.size(); while (curr) { res.emplace_back(INT_MIN); while (curr--) { res.back() = max(res.back(), q.front()->val); if (q.front()->left) q.push(q.front()->left); if (q.front()->right) q.push(q.front()->right); q.pop(); } curr = q.size(); } return res; }};
阅读全文
0 0
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- LeetCode 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- week3-NO.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
- adb 安装 app 失败,提示[INSTALL_CANCELED_BY_USER]
- Java编程思想之操作符
- 给 SwitchCompat 设置颜色的方法
- ecliple-tomcat部署maven项目方式之三
- (半)欧拉图,欧拉路径、回路学习小记
- 515. Find Largest Value in Each Tree Row
- 连续子数组的最大和
- Java多线程的常见例子
- 腾讯云centos7搭建javaweb服务器(本人亲自经历,详细)
- CSDN资料要完善到什么程度才算100%,就差5%了!http://img.blog.csdn.net/20170705200409612
- LeetCode 18. 4Sum
- Android studio 事件总线RxBus学习总结
- 微信公众号相关的设置
- 2017.07.05回顾