Leet Code 515. Find Largest Value in Each Tree Row【一个stack记录层次】numeric——limits<int>max
来源:互联网 发布:运动健身软件 编辑:程序博客网 时间:2024/04/28 14:48
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]
#include<iostream>#include<vector>#include<unordered_map>#include<queue>using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution {public:int singleNumber(vector<int>& nums) {//方法1/*unordered_map<int, int> m;for (int i = 0; i < nums.size(); i++){if (m.find(nums[i]) != m.end()){m[nums[i]]++;}else{m.insert({ nums[i], 1 });}}unordered_map<int, int> ::iterator itr = m.begin();while (itr != m.end()){if (itr->second != 2)return itr->first;itr++; }*///方法2int result = 0;for (int i = 0; i < nums.size(); i++){result = result ^ nums[i];}return result;}vector<int> largestValues(TreeNode* root) {//queue<int>q1;queue<TreeNode> q2;q2.push(*root);//q1.push(1);vector<int> ans;while (!q2.empty()){int size = q2.size();int maxval = (numeric_limits<int>::min)();for (int i = 0; i < size; i++){TreeNode treetmp = q2.front();if (treetmp.val > maxval)maxval = treetmp.val; q2.pop();//q1.pop();if (treetmp.left != NULL){q2.push(*treetmp.left);}if (treetmp.right != NULL){q2.push(*treetmp.right);}}ans.push_back(maxval);}return ans;}};int main(){Solution s;TreeNode n1(1);TreeNode n2(3);TreeNode n3(2);TreeNode n4(5);TreeNode n5(3);TreeNode n6(9);n1.left = &n2;n1.right = &n3;n2.left = &n4;n2.right = &n5;n3.right = &n6;vector<int> v = s.largestValues(&n1);return 0;}
0 0
- Leet Code 515. Find Largest Value in Each Tree Row【一个stack记录层次】numeric——limits<int>max
- Tree——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
- week3-NO.515. Find Largest Value in Each Tree Row
- LeetCode笔记:515. Find Largest Value in Each Tree Row
- 深入浅出 RPC - 浅出篇
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F
- 1021 石子归并 (区间dp)
- unityVR项目整合入iOS主程序
- 欢迎使用CSDN-markdown编辑器
- Leet Code 515. Find Largest Value in Each Tree Row【一个stack记录层次】numeric——limits<int>max
- MessageFormat.format的方法测试
- 超融合架构与容器超融合
- 如何实例化i2c_client(四法)
- HTTP协议详解
- Unity开发详解之灯光和摄像机(2/6)
- 仿微信、QQ实现多图照片上传、照相,照片预览
- MAVEN学习开发问题记录帖(持续更新...)
- 活动被回收时临时数据的处理方法