O(N) Time and Space by doing Preprocessing of level and maxHeight
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nO(N) Time and Space by 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nO(N) Time and Space by doing Preprocessing of level and maxHeight\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n# Code\n```golang []\n/**\n * Definition for a binary tree node.\n * type TreeNode struct {\n *     Val int\n *     Left *TreeNode\n *     Right *TreeNode\n * }\n */\n\nfunc findHeightOfNodes(root *TreeNode, l int, height, levelMaxHeight, levelSecondMaxHeight, level []int) int{\n\n    if root == nil {\n        return 0\n    }\n\n    level[root.Val] = l //level Population / preprocessing\n    height[root.Val] = max(findHeightOfNodes(root.Left, l+1, height, levelMaxHeight, levelSecondMaxHeight, level), findHeightOfNodes(root.Right, l+1, height, levelMaxHeight, levelSecondMaxHeight, level)) + 1\n\n    //max and second max height calculation as if node that is to be deleted has the max height from that node\n\n    if levelMaxHeight[l] < height[root.Val]{\n        levelSecondMaxHeight[l] = levelMaxHeight[l]\n        levelMaxHeight[l] = height[root.Val]\n    } else if levelSecondMaxHeight[l] < height[root.Val]{\n        levelSecondMaxHeight[l] = height[root.Val]\n    }\n\n    return height[root.Val]\n}\nfunc treeQueries(root *TreeNode, queries []int) []int {\n    const n = 1000001\n    height := make([]int, n)\n    levelMaxHeight := make([]int, n)\n    levelSecondMaxHeight := make([]int, n)\n    level := make([]int, n)\n\n    findHeightOfNodes(root, 0, height, levelMaxHeight, levelSecondMaxHeight, level)\n\n    ans := make([]int, len(queries))\n\n    for i, query := range queries{\n        levelOfNode := level[query]\n\n        if levelMaxHeight[levelOfNode] == height[query]{ //node to be deleted is max height node so use height as secondMaxHeight node\n            ans[i] = levelOfNode + levelSecondMaxHeight[levelOfNode] - 1\n        }else{\n            ans[i] = levelOfNode + levelMaxHeight[levelOfNode] - 1\n        }\n    }\n\n    return ans\n}\n```
['Go']