/**
* 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>> ans;
queue<TreeNode*> q[2];
int f=0;
if(root) q[f].push(root);
while(!q[f].empty()){
vector<int> tmp;
while(!q[f].empty()){
auto x=q[f].front();q[f].pop();
tmp.push_back(x->val);
if(x->left) q[f^1].push(x->left);
if(x->right) q[f^1].push(x->right);
}
ans.push_back(tmp);
f^=1;
}
reverse(ans.begin(),ans.end());
return ans;
}
};
Leetcode 107. 二叉树的层次遍历 II
最新推荐文章于 2019-06-10 10:59:17 发布