Precompute height (level) & Depth for each node | Beats 83%
\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.lef
```\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        """\n        for every node, store the level and max depth of it\'s subtree\n        for every level, store the highest and second highest depth\n        """\n        depth = {}\n        level = {}\n        max_depth_at_each_level = {}\n        def compute(root, height):\n            if not root:\n                return 0\n            level[root.val] = height\n            left = compute(root.left, height+1)\n            right = compute(root.right, height+1)\n            depth[root.val] = max(left, right)\n            return 1 + max(left, right)\n        compute(root, 0)\n\n        def compute_max_depth_at_each_level(root):\n            que = deque([root])\n            level = 0\n            while len(que):\n                for _ in range(len(que)):\n                    node = que.popleft()\n                    x = depth[node.val]\n                    if level in max_depth_at_each_level:\n                        max_depth_at_each_level[level].append(x)\n                    else:\n                        max_depth_at_each_level[level] = [x]\n                    if node.left:\n                        que.append(node.left)\n                    if node.right:\n                        que.append(node.right)\n                max_depth_at_each_level[level].sort(reverse=True)\n                level+=1\n        compute_max_depth_at_each_level(root)\n\n        ans = []\n        for query in queries:\n            del_lev = level[query]\n            if len(max_depth_at_each_level[del_lev]) == 1:\n                ans.append(del_lev-1)\n                continue\n            if depth[query] == max_depth_at_each_level[del_lev][0]:\n                ans.append(del_lev + max_depth_at_each_level[del_lev][1])\n            else:\n                ans.append(del_lev + max_depth_at_each_level[del_lev][0])\n        return ans\n```
['Depth-First Search', 'Breadth-First Search', 'Python3']