title: Passing value from vector pair of struct pointer to function
body: <p>The code is about finding diameter of a tree where nodes are connected by edges. I'm facing problem when I call the diameter function. It compiles without error, but when I run the code, the code stops its execution after calling the diameter function.</p>
  <pre><code>#include &lt;bits/stdc++.h&gt;
  using namespace std;
  struct Node {
      int data;
      struct Node* left,*right;
  };
  
  int height(struct Node* node){
      if(node==NULL) return 0;
      else return max(height(node-&gt;left),height(node-&gt;right))+1;
  }
  int diameter(struct Node* node){
      if(node==NULL){
         return 0;
      }
      int left_height=height(node-&gt;left);
      int right_height=height(node-&gt;right);
      return max(left_height+right_height+1,max(diameter(node-&gt;left),diameter(node-&gt;right)));
  
  }
  
  int main(){
      int n,t1,t2;
      cin&gt;&gt;n;
      vector&lt;pair&lt;int,int&gt;&gt;v;
      for(int i=1;i&lt;n;i++){
          cin&gt;&gt;t1&gt;&gt;t2;
          v.push_back({t1,t2});
      }
      vector&lt;pair&lt;struct Node*,struct Node*&gt;&gt;v2;
      for(int i=0;i&lt;n-1;i++){
          struct Node* node1=new Node,*node2=new Node;
          node1-&gt;data=v[i].first; node2-&gt;data=v[i].second;
          node1-&gt;left=NULL; node2-&gt;right=NULL;
          node1-&gt;right=node2; node2-&gt;left=node1;
          v2.push_back({node1,node2});
          // v2[i].first=node1; v2[i].second=node2;
      }
      cout&lt;&lt;&quot;hei&quot;&lt;&lt;endl;
      for(int i=0;i&lt;(signed)v2.size();i++){
          cout&lt;&lt;v2[i].first-&gt;data&lt;&lt;&quot; &quot;&lt;&lt;v2[i].second-&gt;data&lt;&lt;endl;
      }
      // return 0;
      int dia=0,maxdia=0;
      for(auto it : v2){
          dia=diameter(it.first);
          if(dia&gt;maxdia) maxdia=dia;
          dia=diameter(it.second );
          if(dia&gt;maxdia) maxdia=dia;
      }
      cout&lt;&lt;endl;
      cout&lt;&lt;&quot;anything&quot;&lt;&lt;endl;
  
      cout&lt;&lt;maxdia&lt;&lt;endl;
      return 0;
  }
  
  /* inputs 
  //Input1
  3
  3 2
  2 1
  //Input2
  5
  4 2
  1 4
  5 4
  3 4
  
  */
  </code></pre>
  <p>The diameter function works fine by manual inputs</p>
  <pre><code>
  struct Node* NewNode(int data){
      struct Node* node=new Node();
      node-&gt;data=data;
      node-&gt;left=NULL; node-&gt;right=NULL;
  }
  
  int main(){
      struct Node* root= NewNode(1);
      root-&gt;left=     NewNode(2);
      root-&gt;right=    NewNode(3);
      root-&gt;left-&gt;right=    NewNode(4);
      root-&gt;left-&gt;left=     NewNode(5);
      // root-&gt;right-&gt;left=    NewNode(6);
      root-&gt;left-&gt;left-&gt;right=    NewNode(7);
      root-&gt;left-&gt;right-&gt;right    =NewNode(8);
      root-&gt;left-&gt;right-&gt;right-&gt;left    =NewNode(9);
      root-&gt;left-&gt;right-&gt;right-&gt;left-&gt;right    =NewNode(10);
      cout&lt;&lt;diameter(root)&lt;&lt;endl;
      return 0;
  }
  </code></pre>