当前位置:网站首页>LeetCode 0107. Sequence traversal of binary tree II - another method
LeetCode 0107. Sequence traversal of binary tree II - another method
2022-07-05 06:10:00 【Tisfy】
【LetMeFly】107. Sequence traversal of binary tree II
Force button topic link :https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/
Give you the root node of the binary tree root , Returns its node value Bottom up sequence traversal . ( That is, from the layer where the leaf node is to the layer where the root node is , Traversal layer by layer from left to right )
Example 1:

Input :root = [3,9,20,null,null,15,7] Output :[[15,7],[9,20],[3]]
Example 2:
Input :root = [1] Output :[[1]]
Example 3:
Input :root = [] Output :[]
Tips :
- The number of nodes in the tree is in the range
[0, 2000]Inside -1000 <= Node.val <= 1000
Method 1 : Priority queue
And The previous method Different , This time I decided not to use pair<TreeNode*, int>, But directly TreeNode* The team .
If you don't understand, you can read the previous blog first :https://letmefly.blog.csdn.net/article/details/125584554
that , period int Number of layers of type , How to determine which nodes belong to the same layer ?
It's also very simple , When the queue is not empty , Record the total number of elements in the queue , The number of these elements is the number of nodes in the current last layer .
then , We use a cycle , Add all these elements to the same layer of the answer ( At the same time, join the child nodes ) that will do .
- Time complexity O ( N ) O(N) O(N), among N N N Is the number of nodes .
- Spatial complexity O ( N 2 ) O(N2) O(N2), among N 2 N2 N2 Is the number of nodes in the layer with the most nodes .
AC Code
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()); // Be careful , Here is the question, which requires you to traverse from the last level , therefore reverse The following . Under normal circumstances, the level traversal does not need reverse Of
return ans;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125610699
边栏推荐
- Appium automation test foundation - Summary of appium test environment construction
- LaMDA 不可能觉醒吗?
- leetcode-6109:知道秘密的人数
- Arduino 控制的 RGB LED 无限镜
- Leetcode-6111: spiral matrix IV
- SQLMAP使用教程(二)实战技巧一
- 【Rust 笔记】13-迭代器(中)
- A reason that is easy to be ignored when the printer is offline
- shared_ Repeated release heap object of PTR hidden danger
- leetcode-6108:解密消息
猜你喜欢

leetcode-6110:网格图中递增路径的数目

Introduction and experience of wazuh open source host security solution

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

网络工程师考核的一些常见的问题:WLAN、BGP、交换机

Appium自动化测试基础 — Appium测试环境搭建总结

做 SQL 性能优化真是让人干瞪眼

Open source storage is so popular, why do we insist on self-development?
![[practical skills] how to do a good job in technical training?](/img/a3/7a1564cd9eb564abfd716fef08a9e7.jpg)
[practical skills] how to do a good job in technical training?

LeetCode 0107.二叉树的层序遍历II - 另一种方法

Spark中groupByKey() 和 reduceByKey() 和combineByKey()
随机推荐
Leetcode-31: next spread
[jailhouse article] look mum, no VM exits
实时时钟 (RTC)
R语言【数据集的导入导出】
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
leetcode-1200:最小绝对差
QT判断界面当前点击的按钮和当前鼠标坐标
Daily question 1984 Minimum difference in student scores
Simply sort out the types of sockets
1041 Be Unique
【Rust 笔记】17-并发(上)
CF1637E Best Pair
Daily question 2013 Detect square
The connection and solution between the shortest Hamilton path and the traveling salesman problem
leetcode-6108:解密消息
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
SQLMAP使用教程(二)实战技巧一
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
shared_ Repeated release heap object of PTR hidden danger
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}