Explaination With Dry Run C++ well commented code DFS+Hashmap
Solution:\n1. For removing any node we need to keep track of height of CurrNode(Node that is supposed to be removed) and two max heights at same level of that n
Solution:\n1. For removing any node we need to keep track of height of CurrNode(Node that is supposed to be removed) and two max heights at same level of that node.\n\n2. Now there can be two cases\n\t1. \tCurrNode has the maxHeight at that level- removing currNode will affect the total height(Node is in the longest path to leaf i.e EULER TOUR). \n\t**ans[queries[i]]=totalHeight-Height(currNode)+Height(2nd max height at L)**.\n\t**if there is only one node at Level L then consider maxHeight at L-1.**\n\t\n\t2. \tSome other Node has maxheight- removing currNode will not affect the total height beacuse currNode is not in the euler tour(Longest path to leaf) **ans[queries[i]]=totalHeight**\n\n3.Now we need **height**(longest path from currNode to leaf) and **level**(depth from root node) of currNode.\n\n4.Also we need two values  {maxHeight  , 2nd maxHeight } at level L. (use minheap priority queue)\n5.we can compute above values using single DFS call\n6.one IMP thing to understand for beginner is top element of minheap will always be 2nd maxHeight we need to swap (a,b) where **a=maxheight and b=2nd MaxHeight**\n\n![image](https://assets.leetcode.com/users/images/b0d14e96-1ed3-4da4-9ea1-8f37ac0a9a4c_1667370213.3712142.jpeg)\n\n![image](https://assets.leetcode.com/users/images/8f55e85c-a88d-4db0-a556-728a24a62de6_1667370239.3102856.jpeg)\n\n\n**Please Upvote if you like the solution**\n```\nclass Solution {\npublic:\n    map<int,int>height;//node ->height\n    map<int,int>depth;//node ->depth\n    map<int,priority_queue <int, vector<int>, greater<int>> >twomax;//level-> <firstmax,secondmax (H)>\n\n    int dfsHeightDepth(TreeNode* root,int level){\n\n        if(!root) return -1;\n        //recursive calls for calculating height for left and right subtree\n        int lheight=dfsHeightDepth(root->left,level+1);\n        int rheight=dfsHeightDepth(root->right,level+1);\n        \n        //store max heigit and depth(level) in map\n        height[root->val]=max(lheight,rheight)+1;\n        depth[root->val]=level;\n        \n        //maitain priority queue with only 2 elements in it top element will be Node with 2nd max height at perticular level\n        //2nd node will be max height at peritcular level because it is minheap\n        twomax[level].push(height[root->val]);\n        //remove extra nodes from pq\n        if(twomax[level].size()>2) twomax[level].pop();\n        \n        //return computed height\n        return height[root->val];\n        \n\n    }\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        \n        //ans vector\n        vector<int>ans(queries.size());\n        \n        //compute height and depth(level) of tree\n        int h=dfsHeightDepth(root,0);//calculates height and depth of each node\n        \n        for(int i=0;i<queries.size();i++){\n            //for each query Node calculate level and height\n            int level=depth[queries[i]];\n            int heightOfcurrentNode=height[queries[i]];\n            \n            int a=-1,b=-1;\n            if(twomax[level].size()==2){\n                //beacuse it is minheap top element will be smaller\n                b=twomax[level].top();twomax[level].pop();\n                a=twomax[level].top();twomax[level].pop();\n                \n                // if currentNode has maxheight then remove its height from total height and add height of 2nd Max Node to total height\n                if(heightOfcurrentNode==a) ans[i]=h-(a-b); \n                \n                //if it is not maxheight node then it should not affect total height computation\n                else ans[i]=h;  \n                \n                //again add popped element back to priority queue\n                twomax[level].push(b);\n                twomax[level].push(a);\n            }\n            \n            // if only one node present at that level subtract the heightOfcurrentNode and consider height of (level-1)th max height\n            else ans[i]=h-heightOfcurrentNode-1;\n            \n              \n        }\n    \n        return ans;\n    }\n};\n```\n**Please Upvote if you like the solution**
['Depth-First Search', 'C', 'Heap (Priority Queue)']