class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> res;
lVhelp(root,res,0);
return res;
}
void lVhelp(TreeNode *r,vector<int> &res,int depth){
if(r==NULL) return;
if(depth+1>res.size()) res.push_back(0x80000000);
res[depth]=max(r->val,res[depth]);
lVhelp(r->left,res,depth+1);
lVhelp(r->right,res,depth+1);
}
};
515. 在每个树行中找最大值
最新推荐文章于 2022-07-07 11:10:01 发布
本文介绍了一种算法,该算法能够找出二叉树每一行的最大值。通过递归方式遍历树节点,并记录每层的最大值,最终返回包含各层最大值的列表。此方法适用于需要快速获取树形结构中每层关键数据的情况。
5428

被折叠的 条评论
为什么被折叠?



