/** * 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> > result; if(root == NULL) return result; deque<TreeNode*> list; list.push_back(root); deque<TreeNode*> templist; vector<int> temp; while(!list.empty()) { temp.clear(); templist.clear(); for(deque<TreeNode*>::iterator iter = list.begin();iter!=list.end();iter++) { temp.push_back((*iter)->val); if((*iter)->left!=NULL) templist.push_back((*iter)->left); if((*iter)->right!=NULL) templist.push_back((*iter)->right); } result.push_back(temp); list = templist; } return result; } };