C++ | DFS + BFS | O(n)
Code

# Code
```cpp []
class Solution {
public:
    unordered_map<int,int>height;
    int dfs(TreeNode *root, int level)
    {
        if(root==NULL)
           return 0;
        int leftHeight=dfs(root->left, level+1);
        int rightHeight=dfs(root->right, level+1);
        int currMax=max(level, leftHeight);
        currMax=max(currMax, rightHeight);
        height[root->val]=currMax;
        return currMax;   
    }
    vector<int> treeQueries(TreeNode* root, vector<int>& queries) 
    {
        dfs(root, 0);
        unordered_map<int,int>heightAfterRemoval;
        queue<TreeNode *>q;
        int level=0;
        q.push(root);
        while(!q.empty())
        {
            int size=q.size(),levelMaxHeight=level-1, levelSecondMaxHeight=0;
            vector<int>nodes;

            while(size--)
            {
                TreeNode *node=q.front();
                q.pop();
                nodes.push_back(node->val);
                if(height[node->val]>levelMaxHeight)
                {
                    levelSecondMaxHeight=levelMaxHeight;
                    levelMaxHeight=height[node->val];
                }
                else if(height[node->val]>levelSecondMaxHeight)
                    levelSecondMaxHeight=height[node->val];
                if(node->left)
                    q.push(node->left);
                if(node->right)
                    q.push(node->right);    
            }
            for(auto node:nodes)
            {
                if(height[node]!=levelMaxHeight)
                    heightAfterRemoval[node]=levelMaxHeight;
                else
                    heightAfterRemoval[node]=levelSecondMaxHeight;     
            }
            level++;
        }

        vector<int> res;
        for(auto &q:queries)
           res.push_back(heightAfterRemoval[q]);
        return res;   
    }
};
```
['C++']