One of C++ solutions (preorder)
vector<vector<int>> ret;\n    \n    void buildVector(TreeNode *root, int depth)\n    {\n        if(root == NULL) return;\n        if(ret.size() == depth)\n     
    vector<vector<int>> ret;\n    \n    void buildVector(TreeNode *root, int depth)\n    {\n        if(root == NULL) return;\n        if(ret.size() == depth)\n            ret.push_back(vector<int>());\n        \n        ret[depth].push_back(root->val);\n        buildVector(root->left, depth + 1);\n        buildVector(root->right, depth + 1);\n    }\n\n    vector<vector<int> > levelOrder(TreeNode *root) {\n        buildVector(root, 0);\n        return ret;\n    }
[]