题目地址:
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/
对一棵二叉树进行层序遍历,并从下到上返回每一层的值。
代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
queue<TreeNode*> q;
q.push(root);
while (q.size()) {
vector<int> row;
for (int i = q.size(); i; i--) {
auto t = q.front();
q.pop();
row.push_back(t->val);
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
res.push_back(std::move(row));
}
reverse(res.begin(), res.end());
return res;
}
};
时空复杂度 O ( n ) O(n) O(n)。