Compute once, O(n)
\nDfs to calculate and store the maximum depth reachable from each node.\nThen answer each query with the precomputed maximum depth for the specified node.\n\nk
\nDfs to calculate and store the maximum depth reachable from each node.\nThen answer each query with the precomputed maximum depth for the specified node.\n\n```kotlin []\nclass Solution {\n    val heightMap = mutableMapOf<TreeNode?, Int>(null to 0)\n    val res = mutableMapOf<Int, Int>()\n    \n    fun treeQueries(root: TreeNode?, queries: IntArray): IntArray {\n        dfs(root, 0, 0)\n        return queries.map { res[it] ?: 0 }.toIntArray()\n    }\n\n    fun dfs(node: TreeNode?, depth: Int, max: Int) {\n        if (node == null) return\n        res[node.`val`] = max\n\n        dfs(node.left, depth + 1, max(max, depth + height(node.right)))\n        dfs(node.right, depth + 1, max(max, depth + height(node.left)))\n    }\n\n    fun height(node: TreeNode?): Int = heightMap.getOrPut(node) { \n        1 + maxOf(height(node?.left), height(node?.right)) }\n}\n```
['Kotlin']