您的位置:首页 > 产品设计 > UI/UE

Leetcode: Binary Tree Level Order Traversal: Two Queue

2013-01-08 00:59 441 查看
/**
* 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> > levelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > vv;
if(!root)
return vv;
vv.clear();
vector<int> v;
v.clear();
queue<TreeNode*> current,next;
current.push(root);
while(!current.empty()){
TreeNode* tmp=current.front();
current.pop();
v.push_back(tmp->val);
if(tmp->left)
next.push(tmp->left);
if(tmp->right)
next.push(tmp->right);
if(current.empty()){
vv.push_back(v);
v.clear();
swap(current,next);
}
}
return vv;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: