getting compile error maybe something wrong in vector implementation
problem link:
my solution link:
Leetcode level order traversal
Your Modified Solution
this Solution will give TLE
try out the optimized Solution given below
Correct Solution
class Solution {
public:
void buildVector(vector<vector<int>> & v, TreeNode* node, int depth)
{
if (node == NULL)
return;
if (v.size() == depth)
v.push_back(vector<int>());
v[depth].push_back(node->val);
buildVector(v, node->left, depth+1);
buildVector(v, node->right, depth+1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
buildVector(res, root, 0);
return res;
}
};