title: 9147 Segmentation Fault : 11 Construct Binary tree from inorder and preorder traversals
body: <p>Given inorder and preorder traversal of a Binary Tree, construct the Binary Tree.</p>
  <p>While constructing a tree from inorder and preorder traversal, i think i have written it properly  but why is it giving a segmentatation fault. Please help.
  below is my code for the same :</p>
  <pre><code>#include&lt;bits/stdc++.h&gt;
  using namespace std;
  
  class Node {
   public :
   int data;
   Node* left;
   Node* right;
  
   Node(int data)
   {
      this-&gt;data = data;
      left = right = NULL;
   }
  };
  
  void Inorder(Node* root)
  {
    if (root == NULL)
    {
      return;
    }
  
    Inorder(root-&gt;left);
    cout &lt;&lt; root-&gt;data &lt;&lt; &quot; &quot;;
    Inorder(root-&gt;right);
  }
  
   Node* buildTreeHelper( int inorder[], int preorder[], int InS, int InE, int PreS, int PreE)
   {
  
    if (InS &gt; InE)
    {
       return NULL;
    }
  
   int rootData = preorder[PreS];
  
   int lIns = InS;
   int rootIndex = -1;
   for (int i = InS ; i &lt; InE ; i++)
    {
       if (inorder[i] == rootData)
         {
           rootIndex = i;
           break;
         }
    }
      int lIne = rootIndex - 1;
      int lPres = PreS + 1;
      int lPree = lIne - lIns + lPres;
      int rIns = rootIndex + 1;
      int rIne = InE;
      int rPres = lPree  + 1;
      int rPree = PreE;
  
  
     Node* root = new Node(rootData);
     root-&gt;left = buildTreeHelper(inorder, preorder, lIns, lIne, lPres, lPree);
     root-&gt;right = buildTreeHelper(inorder, preorder, rIns, rIne, rPres, rPree);
  
      return root;
  }
  
  Node* buildTree(int inorder[], int preorder[], int size)
  {
      return buildTreeHelper(inorder, preorder, 0, size - 1, 0, size - 1);
  }
  int main()
  {
  
      int inorder[] = {4, 2, 5, 1, 8, 6, 9, 3, 7};
      int preorder[] = {1, 2, 4, 5, 3, 6, 8, 9, 7};
  
     Node* root = buildTree(inorder, preorder, 9);
      Inorder(root);
      delete root;
      return 0;
  }
  </code></pre>
  <p>I am unable to identify what's the error, please help me out of this.</p>