当前位置:网站首页>【515. 在每个树行中找最大值】
【515. 在每个树行中找最大值】
2022-06-25 07:19:00 【Sugar_wolf】
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
示例1:

输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
示例2:
输入: root = [1,2,3]
输出: [1,3]
提示:
- 二叉树的节点个数的范围是 [0,104]
- -231 <= Node.val <= 231 - 1
方法一:深度优先搜索
思路与算法
我们用树的「先序遍历」来进行「深度优先搜索」处理,并用 \textit{curHeight}curHeight 来标记遍历到的当前节点的高度。当遍历到 \textit{curHeight}curHeight 高度的节点就判断是否更新该层节点的最大值。
代码:
class Solution {
public:
void dfs(vector<int>& res, TreeNode* root, int curHeight) {
if (curHeight == res.size()) {
res.push_back(root->val);
} else {
res[curHeight] = max(res[curHeight], root->val);
}
if (root->left) {
dfs(res, root->left, curHeight + 1);
}
if (root->right) {
dfs(res, root->right, curHeight + 1);
}
}
vector<int> largestValues(TreeNode* root) {
if (!root) {
return {
};
}
vector<int> res;
dfs(res, root, 0);
return res;
}
};
执行用时:8 ms, 在所有 C++ 提交中击败了80.00%的用户
内存消耗:21.4 MB, 在所有 C++ 提交中击败了94.52%的用户
复杂度分析
时间复杂度: O(n),其中 n 为二叉树节点个数。二叉树的遍历中每个节点会被访问一次且只会被访问一次。
空间复杂度: O(height)。其中 height 表示二叉树的高度。递归函数需要栈空间,而栈空间取决于递归的深度,因此空间复杂度等价于二叉树的高度。
方法二:广度优先搜索
思路与算法
我们也可以用「广度优先搜索」的方法来解决这道题目。「广度优先搜索」中的队列里存放的是「当前层的所有节点」。每次拓展下一层的时候,不同于「广度优先搜索」的每次只从队列里拿出一个节点,我们把当前队列中的全部节点拿出来进行拓展,这样能保证每次拓展完的时候队列里存放的是下一层的所有节点,即我们是一层一层地进行拓展,然后每一层我们用 maxVal 来标记该层节点的最大值。当该层全部节点都处理完后, maxVal 就是该层全部节点中的最大值。
代码:
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if (!root) {
return {
};
}
vector<int> res;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int len = q.size();
int maxVal = INT_MIN;
while (len > 0) {
len--;
auto t = q.front();
q.pop();
maxVal = max(maxVal, t->val);
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
res.push_back(maxVal);
}
return res;
}
};
执行用时:8 ms, 在所有 C++ 提交中击败了80.00%的用户
内存消耗:21.5 MB, 在所有 C++ 提交中击败了88.01%的用户
复杂度分析
时间复杂度: O(n),其中 n 为二叉树节点个数,每一个节点仅会进出队列一次。
空间复杂度: O(n),存储二叉树节点的空间开销。
author: LeetCode-Solution
边栏推荐
- How to analyze the grey prediction model?
- Word2vec, phrases, phraser, keyedvectors commonly used in gensim
- TCP MIN_ A dialectical study of RTO
- TCP acceleration notes
- Exchange:管理日历权限
- Go language learning tutorial (13)
- Websocket understanding and application scenarios
- 配置、软件配置项、软件配置管理项辨析
- 如何成为一名软件测试高手? 月薪3K到17K,我做了什么?
- InfluxDB时序数据库
猜你喜欢

Establish open data set standards and enable AI engineering implementation

Deep learning series 45: overview of image restoration

Unity addressable batch management

Use pytorch to build mobilenetv2 and learn and train based on migration

TCP and UDP

How to calculate the D value and W value of statistics in normality test?

Retrieval model rough hnsw

Scanpy (VII) spatial data analysis based on scanorama integrated scrna seq

UEFI:修复 EFI/GPT Bootloader

With the beauty of technology enabled design, vivo cooperates with well-known art institutes to create the "industry university research" plan
随机推荐
Iframe is simple to use, iframe is obtained, iframe element value is obtained, and iframe information of parent page is obtained
在网上股票开户安全吗?证券账户可以给别人用吗?
TrendMicro:Apex One Server 工具文件夹
Prepare these before the interview. The offer is soft. The general will not fight unprepared battles
How to calculate critical weight indicators?
QSS 不同风格的按钮
在哪个平台买股票开户安全?求分享
What is SKU and SPU? What is the difference between SKU and SPU
Rqt command
GIL问题带来的问题,解决方法
Free SSL certificate acquisition tutorial
Log in to MySQL 5.7 under ubuntu18 and set the root password
SharePoint:SharePoint 2013 with SP1 简易安装
Similarity calculation method
Apache CouchDB Code Execution Vulnerability (cve-2022-24706) batch POC
Deep learning series 48:deepfaker
现在网上开通股票账号安全吗?
TCP acceleration notes
使用apt-get命令如何安装软件?
[unexpected token o in JSON at position 1 causes and solutions]