/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>>res;
if(root == NULL)
{
return res;
}
queue<TreeNode*>q;
q.push(root);
int curCount = 1; //当前这一层有几个结点
while(!q.empty())
{
vector<int>temp;
int nextCount = 0; //下一层有几个结点
for(int i = 0; i < curCount; i++)
{
TreeNode* cur = q.front();
q.pop();
temp.push_back(cur->val);
if(cur->left != NULL)
{
q.push(cur->left);
nextCount++;
}
if(cur->right != NULL)
{
q.push(cur->right);
nextCount++;
}
}
res.push_back(temp);
curCount = nextCount;
}
int left = 0, right = res.size()-1;
while(left < right)
{
vector<int> temp = res[left];
res[left] = res[right];
res[right] = temp;
left++;
right--;
}
return res;
}
};
[leetcode]107. 二叉树的层次遍历 II
最新推荐文章于 2024-10-07 07:09:00 发布