您的位置:首页 > 其它

【LeetCode】Binary Tree Level Order Traversal II

2014-11-13 11:37 330 查看
/**
* Definition for binary tree
* 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) {
queue<TreeNode *>node,next;
TreeNode *tmp;
vector<vector<int>> result;
vector<int> Inter;
if (root)
{
node.push(root);
}
while (node.size())
{
while (node.size())
{
tmp = node.front();
//		cout << tmp->val << " ";
Inter.push_back(tmp->val);
node.pop();
if (tmp->left)
next.push(tmp->left);
if (tmp->right)
next.push(tmp->right);
}
result.push_back(Inter);
Inter.clear();
swap(node, next);
}
reverse(result.begin(),result.end());//reverse 搞定
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: