1

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.

2

2 Answers 2

1
   vector<vector<int>> output;

This declares a vector, an empty vector. The vector contains absolutely nothing, whatsoever. Its size is 0. This vector then gets passed as a parameter to a function, where the following happens:

   out[level].push_back(root->val);

Since this vector is completely empty, out[level] results in undefined behavior. The [] operator, in C++, must specify an index of an existing element in the vector. For example, when using [4], the underlying vector must have at least five values.

Here, since the vector is empty, no vector index is valid, and the above code results in undefined behavior, and is the likely reason for your crash.

[] (with a std::vector) does not create or add values to a vector. It only accesses existing values in the vector, and it is your responsibility to ensure that the index referenced by the [] operator exists.

Sign up to request clarification or add additional context in comments.

Comments

0
vector<vector<int>> output;

and then

out[level].push_back ...;

You get undefined behavior.

If you create the vector with default constructor, it is zero size and you cannot index it with brackets like this. Use std::vector::resize to resize the vector to the needed size first. You can resize (enlarge) the vector repeatedly without losing the existing content.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.