当前位置:网站首页>LeetCode 0107.二叉树的层序遍历II - 另一种方法
LeetCode 0107.二叉树的层序遍历II - 另一种方法
2022-07-05 05:45:00 【Tisfy】
【LetMeFly】107.二叉树的层序遍历 II
力扣题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/
给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
示例 1:

输入:root = [3,9,20,null,null,15,7] 输出:[[15,7],[9,20],[3]]
示例 2:
输入:root = [1] 输出:[[1]]
示例 3:
输入:root = [] 输出:[]
提示:
- 树中节点数目在范围
[0, 2000]内 -1000 <= Node.val <= 1000
方法一:优先队列
与之前方法不同,这次决定不使用pair<TreeNode*, int>,而是直接使用TreeNode*入队。
看不懂的可以先看之前这篇博客:https://letmefly.blog.csdn.net/article/details/125584554
那么,没有了int类型的层数,怎么判断哪些节点是属于同一层的呢?
其实也很简单,我们在队列不空的时候,记录下来队列中一共由多少个元素,这些元素的个数就是当前最后一层的节点的个数。
然后,我们用一个循环,把这些元素全都添加到答案的同一层中(同时把子节点入队)即可。
- 时间复杂度 O ( N ) O(N) O(N),其中 N N N是节点个数。
- 空间复杂度 O ( N 2 ) O(N2) O(N2),其中 N 2 N2 N2是节点最多的一层的节点数。
AC代码
C++
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
queue<TreeNode*> q;
if (root)
q.push(root);
int layer = -1;
while (q.size()) {
ans.push_back({
});
layer++;
int thisLayerNum = q.size();
while (thisLayerNum--) {
TreeNode* thisNode = q.front();
q.pop();
ans[layer].push_back(thisNode->val);
if (thisNode->left)
q.push(thisNode->left);
if (thisNode->right)
q.push(thisNode->right);
}
}
reverse(ans.begin(), ans.end()); // 注意,这里是本题要求从最后一层开始遍历,所以reverse了以下。正常情况下的层次遍历是不需要reverse的
return ans;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125610699
边栏推荐
- Introduction to convolutional neural network
- Csp-j-2020-excellent split multiple solutions
- Summary of Haut OJ 2021 freshman week
- 2017 USP Try-outs C. Coprimes
- 过拟合与正则化
- 中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
- How to adjust bugs in general projects ----- take you through the whole process by hand
- Introduction and experience of wazuh open source host security solution
- [jailhouse article] jailhouse hypervisor
- kubeadm系列-00-overview
猜你喜欢

【云原生】微服务之Feign自定义配置的记录

从Dijkstra的图灵奖演讲论科技创业者特点

剑指 Offer 53 - I. 在排序数组中查找数字 I

Introduction and experience of wazuh open source host security solution
![[cloud native] record of feign custom configuration of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] record of feign custom configuration of microservices

2017 USP Try-outs C. Coprimes

Sword finger offer 04 Search in two-dimensional array

个人开发的渗透测试工具Satania v1.2更新

Graduation project of game mall

Talking about JVM (frequent interview)
随机推荐
One question per day 1765 The highest point in the map
Sword finger offer 06 Print linked list from beginning to end
Simply sort out the types of sockets
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
EOJ 2021.10 E. XOR tree
剑指 Offer 53 - II. 0~n-1中缺失的数字
【Jailhouse 文章】Look Mum, no VM Exits
Sword finger offer 09 Implementing queues with two stacks
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Configuration and startup of kubedm series-02-kubelet
7. Processing the input of multidimensional features
Over fitting and regularization
Wazuh開源主機安全解决方案的簡介與使用體驗
[practical skills] how to do a good job in technical training?
Acwing 4300. Two operations
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
Drawing dynamic 3D circle with pure C language
Detailed explanation of expression (csp-j 2021 expr) topic
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software