leetcode 515. Find Largest Value in Each Tree Row
来源:互联网 发布:excel数据有效性的来源 编辑:程序博客网 时间:2024/04/30 14:10
Hi,又见面了~第二天,难度和之前差不多。
515. Find Largest Value in Each Tree Row
一.犯的错误
(1)逻辑上的问题都是在调试中发现的,比如这题在没加最后一句push_back的时候,发现输出只有2层的输出,最后一层没输出。然后才发现是最后一个if没走到,所以加了一个push_back
二。 初始max的赋值我naive的认为初始化-1000000够小了,然而输入数据给的竟然是-几十亿。。。所以逻辑理清,max初始赋值为根节点就好啦~其实是比较简单的问题,可以发现~
三. 代码如下
/**
* 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> result;
queue <TreeNode *> q;
queue <int> qlevel;
TreeNode * temp;
int i,level;
q.push(root);
qlevel.push(0);
int m=0;
int max;
if(!root) //最后有个空输入,所以判断一下否则过不去
return result;
max=root->val; //果然厉害~输入数据是负几十亿。。 我当时初始化max为-100000,naive
while(q.size())
{
temp = q.front();
q.pop();
level = qlevel.front();
qlevel.pop();
//cout<<temp->val<<level<<endl;
if(temp->left)
{
q.push(temp->left);
qlevel.push(level+1);
}
if(temp->right)
{
q.push(temp->right);
qlevel.push(level+1);
}
if(level==m)
{
if(temp->val>max)
{
max = temp->val;
}
}
if(level>m)
{
m = level;
result.push_back(max);
max = temp->val;//这里max重新赋值为新一层的第一个节点的值
}
}
result.push_back(max);//因为在处理最后一个锇元素后退出,没有到达最后一个if,也就是max值没有更新到result中
//于是最后再加一个push_back
return result;
}
};
- LeetCode 515. Find Largest Value in Each Tree Row
- LeetCode OJ 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
- 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#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++作业4
- MyBatis和Hibernate的区别
- Android 操作SQLite基本用法
- c语言赋值运算返回值
- 指针的指针 数组指针 指针数组
- leetcode 515. Find Largest Value in Each Tree Row
- pandas入门学习一
- 32位和16位指令集模式自动切换机制
- Broken Keyboard (a.k.a. Beiju Text) UVA
- jdbcUtil工具包
- C语言与MATLAB接口 编程与实例
- C/汇编混合编程接口--MIPS ABI
- 博为峰JavaEE技术文章 —— Hibernate 指南(1)Hbm文件
- 反向代理入门理解