515. Find Largest Value in Each Tree Row
来源:互联网 发布:淘宝开店交费吗 编辑:程序博客网 时间:2024/06/05 08:40
问题描述:
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]
解题思路:
由于是计算每层的最大值,故可以在遍历该层次的时候去比较大小。
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> sum;
if(root==NULL) return sum;
sum.push_back(root->val);
dfs(root->left,1,sum);
dfs(root->right,1,sum);
return sum;
}
void dfs(TreeNode* root,int level,vector<int> &nums)
{
if(root==NULL) return;
else if(nums.size()<level+1)
nums.push_back(root->val);
else nums[level]=max(nums[level],root->val);
dfs(root->left,level+1,nums);
dfs(root->right,level+1,nums);
}
};
阅读全文
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
- 大全
- jboss4.0遇到jstl.jar版本冲突错误
- 存储过程的简易使用
- Color Manipulation——颜色操作
- java中的instanceof用法详解
- 515. Find Largest Value in Each Tree Row
- java logback的配置和使用
- selenium3 python 火狐浏览器 调用函数quit()函数后,浏览器关闭,geckorderiver不退出
- 再次召唤灵魂画手!这个AI把你信手涂鸦都变成人脸
- Android MultiDex 解决 64K 限制,方法数超过65535解决方法
- js简单操作,返回上一页,延时操作
- linux用户空间和内核空间延时函数
- rbx2安装
- Android事件分发机制从源码角度解析