MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / buildBinaryTree

Function buildBinaryTree

cpp/Trees/build_binary_tree.cpp:17–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15 */
16
17TreeNode* 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
28TreeNode* buildSubtree(int left, int right, std::vector<int>& preorder, std::vector<int>& inorder,
29 int& preorderIndex, std::unordered_map<int, int>& inorderIndexesMap) {

Callers

nothing calls this directly

Calls 1

buildSubtreeFunction · 0.70

Tested by

no test coverage detected