Euler Tour Based Solution
So Basically we create with euler tour array with elements and its depth from root, like for tree as shown : \n\t           "1"\n\t\t     " /   \"\n\t\t    "2  
* So Basically we create with euler tour array with elements and its depth from root, like for tree as shown : \n\t*           "1"\n\t\t     " /   \\"\n\t\t    "2      3"\n\t\t   "/"\n\t\t  "4"\n* euler tour array is [(1,0),(2,1),(4,2),(4,2),(2,1),(3,1),(3,1),(1,0)]\n* now lets say if we remove subtree  2 than array from index 1 to 4 is removed so now what is maximum height of tree so basically is the maximum height till from index 0 to 1 (not included) and maximum height from index(5 to 7) so we can maintain a prefix and suffix maximum array upto a particular point \n* LET ME EXPLAIN THE MY ROUGH ALGO NOW\n* create a euler tour array as u can see\n* for each node maintain its starting and ending index in euler tour array \n* pre and suf have the maximum  in prefix till i and suffix from i to end \n* So for a particular query we find starting and ending point of subtree with the element and find prefix maxium till start and suffix maximum till end \nIf u like the explanation please do upvote it motivates me a lot \n```py\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        def solve(node,level):\n            euler_tour.append((node.val,level))\n            if node.left:\n                solve(node.left,level+1)\n            if node.right:\n                solve(node.right,level+1)\n            euler_tour.append((node.val,level))\n        euler_tour=[]\n        solve(root,1)\n        d={}\n        for i,el in enumerate(euler_tour):\n            if el[0] in d:\n                d[el[0]].append(i)\n            else:\n                d[el[0]]=[i]\n        pre=[0]\n        for el,l in euler_tour:\n            pre.append(max(pre[-1],l))\n        suf=[0]\n        for el,l in euler_tour[::-1]:\n            suf.append(max(suf[-1],l))\n        suf=suf[::-1]\n        ans=[]\n        \n        for el in queries:\n            start,end=d[el]\n            ans.append(max(pre[start],suf[end+1])-1)\n        return ans\n\t\t```
['Python']