Errors in recursive function calls to create tree from given traversals

I encountered various errors while implementing the logic for https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ and https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

General solutions for both that passed all test cases:
https://leetcode.com/submissions/detail/526513144/
https://leetcode.com/submissions/detail/526684560/
I am going to list down the errors and will appreciate if someone can clarify them:

  1. When startIndex is passed to the function recursively, the argument startIndex is declared like: "int &startIndex" in the function definition. Running it without & (or without passing it as reference) gives heap overflow.
  2. When I submitted the solution for "construct binary tree from inorder and preorder traversals" initially I kept the function calls as:
root->left=func(.......);
root->right=func(......);

After this got submitted successfully, I exchanged the order in which I was calling, i.e.,

root->right=func(.......);
root->left=func(......);

This gives me heap buffer overflow.

Can someone explain why this is happening?

Comments (0)