C++ || O(N) TC || O(N) SC
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)$$ -->\nO(N) where N = number of nodes\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N) where N = number of nodes\n\n# Code\n```cpp []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int computeHeight(TreeNode* root,int level,unordered_map<int,int> &nodeLevel,unordered_map<int,int> &nodeHeight,unordered_map<int,pair<int,int>> &top2height)\n    {\n        if(!root)\n        {\n            return 0;\n        }\n        int v1= computeHeight(root->left,level+1,nodeLevel,nodeHeight,top2height);\n        int v2= computeHeight(root->right,level+1,nodeLevel,nodeHeight,top2height);\n        nodeLevel[root->val]=level;\n        int height= (v1>v2)?v1+1:v2+1;\n        nodeHeight[root->val]=height;\n        pair<int,int> temp= top2height[level];\n        \n        if(temp.first<height)\n        {\n            temp.second= temp.first;\n            temp.first =height;                \n        }\n        else\n        if(temp.second<height)\n        {\n            temp.second = height;\n        }        \n        top2height[level]= temp;\n        return height;\n    }\n\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        int n=queries.size();\n        unordered_map<int,int> nodeLevel;\n        unordered_map<int,int> nodeHeight;\n        unordered_map<int,pair<int,int>> top2height;\n        //preFillHeight(root,0,top2height);\n        computeHeight(root,0,nodeLevel,nodeHeight,top2height);\n        vector<int> ans(n,0);\n        for(int i=0;i<n;i++)\n        {\n            int node=queries[i];\n            int h=nodeHeight[node];\n            int l =nodeLevel[node];\n            auto x= top2height[l];\n            if(h == x.first)\n            {\n                ans[i]= l+x.second-1;\n               \n            }\n            else\n            {\n                ans[i]= l+x.first-1;              \n            }\n        }\n        return ans;\n\n    }\n};\n```
['C++']