| 15 | */ |
| 16 | |
| 17 | TreeNode* buildBinaryTree(std::vector<int>& preorder, std::vector<int>& inorder) { |
| 18 | // Populate the hash map with the inorder values and their indexes. |
| 19 | std::unordered_map<int, int> inorderIndexesMap; |
| 20 | for (int i = 0; i < inorder.size(); i++) { |
| 21 | inorderIndexesMap[inorder[i]] = i; |
| 22 | } |
| 23 | // Build the tree and return its root node. |
| 24 | int preorderIndex = 0; |
| 25 | return buildSubtree(0, inorder.size() - 1, preorder, inorder, preorderIndex, inorderIndexesMap); |
| 26 | } |
| 27 | |
| 28 | TreeNode* buildSubtree(int left, int right, std::vector<int>& preorder, std::vector<int>& inorder, |
| 29 | int& preorderIndex, std::unordered_map<int, int>& inorderIndexesMap) { |
nothing calls this directly
no test coverage detected