Problem Link - - LeetCode
I am getting a following error on leetcode :-
Line 1034: Char 9: runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator>’ (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/…/lib/gcc/x86_64-linux-gnu/9/…/…/…/…/include/c++/9/bits/stl_vector.h:1043:9
Kindly Suggest a possible correction to my code
My Code :-
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root)
{
vector<vector<int>> ans;
map<int, vector<int>> mp;
if(root == NULL)
return ans;
queue<pair<TreeNode* , int>> q;
q.push({root,0});
while(!q.empty())
{
auto p= q.front();
TreeNode *curr = p.first;
int hd= p.second;
q.pop();
if(root->left != NULL)
{
q.push({root->left, hd-1});
}
if(root -> right != NULL)
{
q.push({root->right, hd+1});
}
ans[hd].push_back(curr->val);
}
return ans;
}
};