当前位置:网站首页>【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
边栏推荐
- Software engineering review questions
- Iframe is simple to use, iframe is obtained, iframe element value is obtained, and iframe information of parent page is obtained
- VOCALOID notes
- QSS 不同风格的按钮
- TCP stuff
- How to analyze the grey prediction model?
- 使用apt-get命令如何安装软件?
- 双周投融报:资本埋伏Web3基础设施
- How to calculate the correlation coefficient and correlation degree in grey correlation analysis?
- Use pytorch to build mobilenetv2 and learn and train based on migration
猜你喜欢
How to analyze the coupling coordination index?
Almost taken away by this wave of handler interview cannons~
微信小程序_7,项目练习,本地生活
Scanpy (VII) spatial data analysis based on scanorama integrated scrna seq
Self made ramp, but it really smells good
Data-centric vs. Model-centric. The Answer is Clear!
以科技赋能设计之美,vivo携手知名美院打造“产学研”计划
Nips 2014 | two stream revolutionary networks for action recognition in videos reading notes
使用apt-get命令如何安装软件?
在二叉树(搜索树)中找到两个节点的最近公共祖先(剑指offer)
随机推荐
Quickly build a real-time face mask detection system in five minutes (opencv+paddlehub with source code)
软件工程复习题
leetcode. 13 --- Roman numeral to integer
Static web server
家庭服务器门户Easy-Gate
打新债安全不 有风险吗
Want to open an account, is it safe to open an online stock account?
在网上股票开户安全吗?证券账户可以给别人用吗?
Daily question brushing record (III)
每日刷题记录 (三)
双周投融报:资本埋伏Web3基础设施
TS environment setup
How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
Measure the current temperature
Find the nearest common ancestor (Sword finger offer) of two nodes in the binary tree (search tree)
Stm32cubemx Learning (5) Input capture Experiment
测一测现在的温度
面试前准备好这些,Offer拿到手软,将军不打无准备的仗
Incluxdb time series database
Getting to know the generation confrontation network (12) -- using pytoch to build wgan-gp to generate handwritten digits