515. Find Largest Value in Each Tree Row
来源:互联网 发布:mac怎么下lol 编辑:程序博客网 时间:2024/06/05 22:50
515. Find Largest Value in Each Tree Row
问题:
找出BTree每一层最大的数
思路:
使用队列,从右向左逐层遍历
code:
/** * 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: vector<int> largestValues(TreeNode* root) { vector<int> lvs; if (root == NULL) return lvs; queue<TreeNode*> que; que.push(root); while (!que.empty()) { int row_size = que.size(); int lv = que.front()->val; for (int i = 0; i < row_size; i++) { TreeNode* r = que.front(); // access the first element que.pop(); // removes the first element if (r->right) que.push(r->right); if (r->left) que.push(r->left); if (r->val > lv) lv = r->val; } lvs.push_back(lv); } return lvs; }};
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
- 《深入理解JAVA虚拟机》学习笔记(五)CLASS文件结构
- 测试总结
- tr命令以及sed命令
- CPU 实模式 保护模式 和虚拟8086模式
- IDEA创建Servlet项目
- 515. Find Largest Value in Each Tree Row
- 6、文件目录详解
- Matlab实现图片批量旋转保存
- 替换字符串中的空格
- 数组的运算
- Decrators
- Linux下git和github的使用
- HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap
- 面向开发的测试技术(一):Mock