/**
* 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;
if (root == NULL)
return ans;
int cnt = 1;
queue<TreeNode*> a[2];
a[1].push(root);
while (a[0].size() || a[1].size())
{
vector<int> tmp;
while (a[cnt].size())
{
root = a[cnt].front();
a[cnt].pop();
if (root->left)
a[(cnt + 1) % 2].push(root->left);
if (root->right)
a[(cnt + 1) % 2].push(root->right);
tmp.push_back(root->val);
}
cnt = (cnt + 1) % 2;
ans.push_back(tmp);
}
vector<int> tmp;
for (int i=0; i<ans.size()/2;++i)
{
tmp = ans[i];
ans[i] = ans[ans.size()-1-i];
ans[ans.size()-1-i] = tmp;
}
return ans;
}
};
level 遍历107. Binary Tree Level Order Traversal II
最新推荐文章于 2018-10-23 17:40:37 发布