515. Find Largest Value in Each Tree Row 找到数每一层的最大值
来源:互联网 发布:淘宝判定为广告的评价 编辑:程序博客网 时间:2024/05/28 05:14
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]这个题目较简单。使用一个队列保存一层树。弹出的过程统计最大值即可
/** * 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> res; if(root==nullptr) return res; queue<TreeNode*> q; q.push(root); while(!q.empty()) { int n=q.size(); int tempmax=numeric_limits<int>::min(); while(n--) { TreeNode* cur=q.front(); q.pop(); if(cur->left) q.push(cur->left); if(cur->right) q.push(cur->right); tempmax=max(tempmax,cur->val); } res.push_back(tempmax); } return res; }};
阅读全文
0 0
- 515. Find Largest Value in Each Tree Row 找到数每一层的最大值
- LeetCode-515. Find Largest Value in Each Tree Row (JAVA)(二叉树每行的最大值)
- Find Largest Value in Each Tree Row(找出一棵树每一行的最大值)
- 【LeetCode515 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
- 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
- 617. Merge Two Binary Trees
- 310. Minimum Height Trees
- 513. Find Bottom Left Tree Value 树 BFS
- 19. Remove Nth Node From End of List
- 浅析JVM类加载机制
- 515. Find Largest Value in Each Tree Row 找到数每一层的最大值
- C++动态分配内存空间 : new 和 delete
- 576. Out of Boundary Paths
- 638. Shopping Offers 深度优先遍历、递归、存储技术(基于递归的动态规划)
- oracle触发器分类
- redis的自启设置
- DFS(深度搜索)——基于回溯法和递归
- Unity学习2 鼠标点击控制物体移动
- C++语法基础之关键字与标识符(10月4日暂存)