Simple Solution using DFS and Hashmap || C++ Fully Explained
Intuition\n Describe your first thoughts on how to solve this problem. \nStore levels of nodes and height of its subtree \n# Approach\n Describe your approach t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStore levels of nodes and height of its subtree \n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int N; // total nodes\n    vector<vector<int>> lv; // this will store level and it\'s maximum heights and only 2 of its point\n    vector<int> nlv; // this will store value of nodes level \n    vector<int> nheg; // this will store height of n node\n    // this function will init all above vectors recursively\n    int trverse(TreeNode* r,int l){\n        if(r == NULL) return 0;\n        // find N \n        N = max(N,r->val);\n        // nlv array will contain r->val level\n        nlv[r->val] = l;\n        // call for left and right\n        int lt = trverse(r->left,l+1);\n        int rt = trverse(r->right,l+1);\n        // nheg will store height of nth node as root node\n        nheg[r->val] = max(lt,rt);\n        // push this node in this level\n        lv[l].push_back(nheg[r->val]);\n        if(lv[l].size() == 3){\n            // find min and remove it\n            int mi = min({lv[l][0],lv[l][1],lv[l][2]});\n            if(lv[l][0] == mi) swap(lv[l][2],lv[l][0]);\n            else if(lv[l][1] == mi) swap(lv[l][2],lv[l][1]);\n            lv[l].pop_back();\n        }\n        return nheg[r->val] + 1;\n    }\n\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        // initalizing size of vectors\n        lv.resize(1e5);\n        nheg.resize(1e5+1,0);\n        nlv.resize(1e5+1,0);\n        // we can store tree node with their value\n        N = 0;\n        nheg[0] = -1;\n        trverse(root,0); // fill all vectors\n        vector<int> t(N+1,0); // store values of height if every node is deleted one time\n        vector<int> ans(queries.size(),0);\n        for(int i=1;i<=N;i++){\n            // try removing i\n            // we are going to search that level\n            if(nlv[i] == 0) continue; // if i is root node\n            int new_maxh = -1; // we have to find new max height\n            bool cnt = 1; // if we have same value for 2 time then avoid only one time\n            // traverse level of i\'th node\n            for(int j : lv[nlv[i]]){\n                if(j == nheg[i] && cnt){ cnt = 0; continue;}\n                new_maxh = max(new_maxh, j);\n            }\n            // if new max is -1 means no other node at this level so this means we can only have tree height \n            // if we found another max then add that max height to this level\n            t[i] = new_maxh == -1 ? nlv[i] - 1 : nlv[i] + new_maxh;\n        }\n        // Now just fill answer for queries\n        for(int i=0;i<queries.size();i++){\n            ans[i] = t[queries[i]];\n        }\n        return ans;\n    }\n};\n```
['Hash Table', 'Depth-First Search', 'C++']