Javascript Beats 100%
Intuition\nGoing through the problem, we ultimately need to traverse the tree for every apple back to the root. Every branch we go to is going to cost 2 movemen
# Intuition\nGoing through the problem, we ultimately need to traverse the tree for every apple back to the root. Every branch we go to is going to cost 2 movement. We need to pay the cost up until the last shared "parent" that exists.\n\n# Approach\nFor easy tree traversal back up, I created a parent map. This is child -> parent.\n\nNext I traverse the hasApple, finding all of the nodes with an apple. If it has an apple, I pay the 2 cost and begin traversing up the tree, until I find a parent for which I have already paid the cost.\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\n/**\n * @param {number} n\n * @param {number[][]} edges\n * @param {boolean[]} hasApple\n * @return {number}\n */\nvar minTime = function(n, edges, hasApple) {\n    const parentMap = new Map();\n    let answer = 0;\n    for (let i = 0; i < edges.length; i++) {\n        parentMap.set(edges[i][1], edges[i][0]);\n    }\n    for (let i = 0; i < hasApple.length; i++) {\n        if (hasApple[i]) {\n            let pointer = i;\n            while (pointer !== 0) {\n                const parent = parentMap.get(pointer);\n                answer += 2;\n                if (hasApple[parent]) {\n                    break;\n                }\n                hasApple[parent] = true;\n                pointer = parent;\n            }\n        }\n    }\n    return answer;\n};\n```
['JavaScript']