[Python 3]Topological sort + Hashmap
```\n# Definition for a binary tree node.\n# class TreeNode:\n#     def init(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left
```\n# Definition for a binary tree node.\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\nclass Solution:\n    def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]:\n        par = {} # keep track of parent nodes\n        height = {} # height for each node\n        \n        def dfs(node, level):\n            if not node:\n                return\n            height[node.val] = level\n            if node.left:\n                par[node.left.val] = node.val\n                dfs(node.left, level+1)\n            if node.right:\n                par[node.right.val] = node.val\n                dfs(node.right, level+1)\n        \n        dfs(root, 0)\n        \n        # Topological sort\n        deg = defaultdict(int)\n        for k in par:\n            deg[par[k]] += 1\n        # starts from leaf nodes    \n        q = [k for k in height if not deg[k]]\n        h = height.copy() # propagate depth to parent nodes\n            \n        while q:\n            cur = q.pop()\n            if cur not in par: continue\n            p = par[cur]\n            deg[p] -= 1\n            h[p] = max(h[p], h[cur])\n            if not deg[p]:\n                q.append(p)\n        \n        # for each level, only store top 2 nodes with deepest path\n        h_node = defaultdict(list)\n        for k in height:\n            heappush(h_node[height[k]], (h[k], k))\n            if len(h_node[height[k]]) > 2:\n                heappop(h_node[height[k]])\n        \n        \n        ans = []\n        for q in queries:\n            # if current level only one node then whole level gone, the height will minus 1\n            if len(h_node[height[q]]) == 1:\n                ans.append(height[q] - 1)\n            # if deleted node equals the node with deepest path at current level, then chose the second deepest\n            elif q == h_node[height[q]][1][1]:\n                ans.append(h_node[height[q]][0][0])\n            # otherwise select the top one\n            else:\n                ans.append(h_node[height[q]][1][0])\n\n        \n        return ans
['Topological Sort', 'Python', 'Python3']