Python3
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\n# Definition for a binary tree node.\n# Uncomment and use the following if needed for testing.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\n\nclass Solution:\n    def treeQueries(self, root: Optional[\'TreeNode\'], queries: List[int]) -> List[int]:\n        depth = {}\n        height = {}\n        nodes = {}\n\n\n        def dfs(node, d):\n            if not node:\n                return -1\n            depth[node.val] = d\n            nodes[node.val] = node\n            left_height = dfs(node.left, d + 1)\n            right_height = dfs(node.right, d + 1)\n            h = max(left_height, right_height) + 1\n            height[node.val] = h\n            return h\n\n        dfs(root, 0)\n\n\n        depth_to_heights = defaultdict(list)\n\n\n        for node_val, d in depth.items():\n            h = height[node_val]\n            depth_to_heights[d].append((h, node_val))\n\n        max_total_height = max(depth.values())\n\n\n        for d in depth_to_heights:\n            depth_to_heights[d].sort(reverse=True)\n\n        res = []\n        for q in queries:\n            q_depth = depth[q]\n            heights_nodes = depth_to_heights[q_depth]\n            if heights_nodes[0][1] != q:\n\n                new_height = max_total_height\n            else:\n                if len(heights_nodes) > 1:\n\n                    second_height = heights_nodes[1][0]\n                    new_height = second_height + q_depth\n                else:\n                    new_height = q_depth - 1\n            res.append(new_height)\n        return res\n\n```
['Python3']