Hello I have this code here:
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> output;
Calculate(root, 0, output);
return output;
}
private:
void Calculate(Node* root, int level, vector<vector<int>>& out)
{
out[level].push_back(root->val);
for(u_int i = 0; i < root->children.size(); i++)
{
if(root->children[i]!=nullptr)
Calculate(root->children[i], level+1, out);
}
}
};
The input is n-ary tree which is of n length. I'm trying to write the values to the corresponding vectors. Every vector is level of the tree and in every of those vectors is another vector which is filled with the numbers from the tree current level.
But when i run the code I get this error: Reference binding to null pointer of type "std::vector<int, std::allocator>"
I'm kinda new to C++ so I have no idea how to solve it. I tried doing it with some logic and asking if the node is null, but that doesn't seem to be the problem.
Here is the problem: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
Thanks for any kind of help.