#include <map>
#include <vector>
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
map<int, vector<int> > levelValues;
void recursivePrint(TreeNode *root, int depth){
if(root == NULL)
return;
if(levelValues.count(root->val) == 0)
levelValues.insert(make_pair(depth, vector<int>()));
levelValues[depth].push_back(root->val);
recursivePrint(root->left, depth+1);
recursivePrint(root->right, depth+1);
}
vector<vector<int> > levelOrder(TreeNode *root) {
recursivePrint(root, 1);
vector<vector<int> > result;
for(map<int,vector<int> >::iterator it = levelValues.begin(); it!= levelValues.end(); ++it){
result.push_back(it->second);
}
return result;
}
};
int main(){
TreeNode node(1);
Solution S;
vector<vector<int> > double_vec;
double_vec = S.levelOrder(&node);
int i=0,j=0;
for(i=0;i<double_vec.size();i++){
for(j=0;j<double_vec[i].size();j++)
cout << double_vec[i][j] << " ";
cout << endl;
}
return 1;
}
I2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBUcmVlTm9kZSB7CiAgICBpbnQgdmFsOwogICAgVHJlZU5vZGUgKmxlZnQ7CiAgICBUcmVlTm9kZSAqcmlnaHQ7CiAgICBUcmVlTm9kZShpbnQgeCkgOiB2YWwoeCksIGxlZnQoTlVMTCksIHJpZ2h0KE5VTEwpIHt9Cn07CiAKY2xhc3MgU29sdXRpb24gewpwdWJsaWM6CiAgICBtYXA8aW50LCB2ZWN0b3I8aW50PiA+IGxldmVsVmFsdWVzOwogICAgdm9pZCByZWN1cnNpdmVQcmludChUcmVlTm9kZSAqcm9vdCwgaW50IGRlcHRoKXsKICAgICAgICBpZihyb290ID09IE5VTEwpCiAgICAgICAgICAgIHJldHVybjsKICAgICAgICBpZihsZXZlbFZhbHVlcy5jb3VudChyb290LT52YWwpID09IDApCiAgICAgICAgICAgIG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