I got accepted with memory limit exceed what modification need to be implemented in my code


#1
#define T TreeNode 

#define l left
#define r right
#define d val

int mx ;

void dfs( int c , map< int , vector > a , int p , int d )
{
mx = max( mx , d );

for( auto x : a[c] )
{
    if( x != p  )dfs( x , a , c , d+1  );
}

}

int Solution::solve(TreeNode* A, int B) {

if( A == nullptr )return 0;

map< int , vector<int> > a;


queue< T* >q ; 
q.push( A );

while( !q.empty() )
{
    T* x = q.front() ; q.pop();
    
    int p = x->d ;
    

    if( x->l != nullptr ){ a[p].push_back( (x->l)->d ); a[(x->l)->d].push_back(p); q.push(x->l); }
    if( x->r != nullptr ){ a[p].push_back( (x->r)->d ); a[(x->r)->d].push_back(p); q.push(x->r); }        
    
    
}

mx = 0;

dfs( B , a , -1 , 0 );

return mx;

}


#2

finally found it replace the map with vector representation , this did the job for me

#define T TreeNode
#define l left
#define r right
#define d val

int mx ;

void dfs( int c , vector a[] , int p , int d )
{
mx = max( mx , d );

for( auto x : a[c] )
{
    if( x != p  )dfs( x , a , c , d+1  );
}

}

int Solution::solve(TreeNode* A, int B) {

if( A == nullptr )return 0;

vector< int >a[100005];



int n = 1 ; 

queue< T* >q ; 
q.push( A );

while( !q.empty() )
{
    T* x = q.front() ; q.pop();
    
    int p = x->d ;
    

    if( x->l != nullptr ){ a[p].push_back( (x->l)->d ); a[(x->l)->d].push_back(p); q.push(x->l); }
    if( x->r != nullptr ){ a[p].push_back( (x->r)->d ); a[(x->r)->d].push_back(p); q.push(x->r); }        
    
    
}

mx = 0;

dfs( B , a , -1 , 0 );

return mx;

}