当前位置:网站首页>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
边栏推荐
- Common optimization methods
- LaMDA 不可能觉醒吗?
- Doing SQL performance optimization is really eye-catching
- Groupbykey() and reducebykey() and combinebykey() in spark
- Implement an iterative stack
- [rust notes] 16 input and output (Part 2)
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- SQLMAP使用教程(一)
- Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
- Daily question 2006 Number of pairs whose absolute value of difference is k
猜你喜欢

Open source storage is so popular, why do we insist on self-development?
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

Appium foundation - use the first demo of appium

Is it impossible for lamda to wake up?

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

LaMDA 不可能觉醒吗?

【Jailhouse 文章】Jailhouse Hypervisor

CF1637E Best Pair

Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135

CF1634 F. Fibonacci Additions
随机推荐
wordpress切换页面,域名变回了IP地址
Control unit
SPI details
1040 Longest Symmetric String
“磐云杯”中职网络安全技能大赛A模块新题
Daily question 1688 Number of matches in the competition
Introduction to convolutional neural network
One question per day 1447 Simplest fraction
1039 Course List for Student
liunx启动redis
Individual game 12
[article de jailhouse] jailhouse hypervisor
[rust notes] 14 set (Part 1)
Data visualization chart summary (I)
MIT-6874-Deep Learning in the Life Sciences Week 7
全排列的代码 (递归写法)
Leetcode-31: next spread
1039 Course List for Student
API related to TCP connection
JS quickly converts JSON data into URL parameters