当前位置:网站首页>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
边栏推荐
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- Sqlmap tutorial (1)
- 6. Logistic model
- MIT-6874-Deep Learning in the Life Sciences Week 7
- Leetcode-556: the next larger element III
- The difference between CPU core and logical processor
- 884. Uncommon words in two sentences
- 开源存储这么香,为何我们还要坚持自研?
- leetcode-9:回文数
- Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
猜你喜欢
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Dynamic planning solution ideas and summary (30000 words)
liunx启动redis
CF1634E Fair Share
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Solution to game 10 of the personal field
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
RGB LED infinite mirror controlled by Arduino
redis发布订阅命令行实现
leetcode-6110:网格图中递增路径的数目
随机推荐
1.13 - RISC/CISC
Fried chicken nuggets and fifa22
[rust notes] 14 set (Part 1)
1.15 - 输入输出系统
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
leetcode-6111:螺旋矩阵 IV
SQLMAP使用教程(一)
【云原生】微服务之Feign自定义配置的记录
Dynamic planning solution ideas and summary (30000 words)
How to adjust bugs in general projects ----- take you through the whole process by hand
CF1634E Fair Share
[rust notes] 17 concurrent (Part 1)
Leetcode-6111: spiral matrix IV
1996. number of weak characters in the game
【实战技能】如何做好技术培训?
Smart construction site "hydropower energy consumption online monitoring system"
[rust notes] 13 iterator (Part 2)
LaMDA 不可能觉醒吗?
[jailhouse article] jailhouse hypervisor
CF1637E Best Pair