/**
* 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) {
vector<vector<int> > result;
if(root == NULL) {
return result;
}
queue<TreeNode *> q;
q.push(root);
while(!q.empty()) {
queue<TreeNode *> temp;
vector<int> toInsert;
while(!q.empty()) {
TreeNode * t = q.front();
q.pop();
toInsert.emplace_back(t->val);
if(t->left != NULL) {
temp.push(t->left);
}
if(t->right != NULL) {
temp.push(t->right);
}
}
result.emplace_back(toInsert);
swap(q, temp);
}
return result;
}
};
4/13/2014
Leetcode -- Binary Tree Level Order Traversal
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment