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

Function buildBinaryTree

kotlin/Trees/BuildBinaryTree.kt:13–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11*/
12
13fun buildBinaryTree(preorder: List<Int>, inorder: List<Int>): TreeNode? {
14 val inorderIndexesMap = hashMapOf<Int, Int>()
15 val preorderIndex = intArrayOf(0)
16 // Populate the hash map with the inorder values and their indexes.
17 for (i in inorder.indices) {
18 inorderIndexesMap[inorder[i]] = i
19 }
20 // Build the tree and return its root node.
21 return buildSubtree(0, inorder.size - 1, preorder, inorder, preorderIndex, inorderIndexesMap)
22}
23
24fun buildSubtree(
25 left: Int,

Callers

nothing calls this directly

Calls 1

buildSubtreeFunction · 0.70

Tested by

no test coverage detected