2-pass preorder traversal
\nfrom collections import defaultdict\n\nclass Solution:\n    def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]:\n        dp = de
```\nfrom collections import defaultdict\n\nclass Solution:\n    def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]:\n        dp = defaultdict(int)\n        def preorder(root, h):\n            if root:\n                dp[root.val] = max(dp[root.val], self.maxheight)\n                self.maxheight = max(self.maxheight, h)\n                preorder(root.left, h + 1)\n                preorder(root.right, h + 1)\n        def preorderrev(root, h):\n            if root:\n                dp[root.val] = max(dp[root.val], self.maxheight)\n                self.maxheight = max(self.maxheight, h)\n                preorderrev(root.right, h + 1)\n                preorderrev(root.left, h + 1)\n        self.maxheight = 0\n        preorder(root, 0)\n        self.maxheight = 0\n        preorderrev(root, 0)\n        res = []\n        for q in queries:\n            res.append(dp[q])\n        return res\n```
['Python']