CPP BFS DFS Easy Solution 🚀
\n# Approach\n Describe your approach to solving the problem. \n1. First calculate level of each node using DFS/BFS any method.\n\n2. Then Calculate What is max
\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. First calculate level of each node using DFS/BFS any method.\n\n2. Then Calculate What is maxDepth from current node i can achieve, for that i used DFS.\n\n3. After that just create an map which can store vector of all pairs of node with their corresponding maxDepth for each level.\n\n4. After that just traverse on that vector and sort all pairs in descending order as we want max Height.\n\n5. Then just traverse on queries vector and find level of that node and traverse the vector for that level to see what is maxDepth i can go with that level.\n\n6. If there is no node for that level simply my ans will be current level of node - 1.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\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 */\nmap<int,int> mp,maxmp;\nclass Solution {\npublic:\n    static bool cmp(pair<int,int> & a, pair<int,int> &b){\n        return a.first > b.first;\n    }\n    void dfs(TreeNode* tmp, int lvl){\n        if(!tmp){\n            return;\n        }\n\n        maxmp[tmp -> val] = lvl;\n        dfs(tmp -> left, lvl + 1);\n        dfs(tmp -> right, lvl + 1);\n        int lft = 0, rgt = 0;\n        lft = tmp -> left ? maxmp[tmp -> left -> val] : 0;\n        rgt = tmp -> right ? maxmp[tmp -> right -> val] : 0;\n        maxmp[tmp -> val] = max({lft,rgt,maxmp[tmp->val]});\n\n    }\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n\n        mp.clear();\n        maxmp.clear();\n        map<int,vector<pair<int,int>>> allLvl;\n\n        queue<pair<TreeNode*,int>> q;\n        q.push({root,0});\n\n        while(!q.empty()){\n            TreeNode* tmp = q.front().first;\n            int lvl = q.front().second;\n            q.pop();\n            mp[tmp -> val] = lvl;\n\n            if(tmp -> left){\n                q.push({tmp -> left, lvl+1});\n            }\n            if(tmp -> right){\n                q.push({tmp -> right, lvl+1});\n            }\n\n        }\n        dfs(root,0);\n\n        for(auto it : maxmp){\n            int node = it.first;\n            int maxdepth = it.second;\n            int lvl = mp[node];\n            allLvl[lvl].push_back({maxdepth,node});\n            // cout<<it.first<< " "<<it.second<<endl;\n        }\n\n        for(auto &it : allLvl){\n            // auto v = it.second;\n            sort(it.second.begin(),it.second.end(),cmp);\n        }\n        vector<int> ans;\n        for(int i : queries){\n            int lvl = mp[i];\n            bool printed = 0;\n            for(auto h : allLvl[lvl]){\n                int depth = h.first;\n                int node = h.second;\n                if(node != i){\n                    ans.push_back(depth);\n                    printed = 1;\n                    break;\n                }\n                // cout<<h\n            }\n            if(!printed){\n                ans.push_back(lvl-1);\n            }\n        }\n\n        for(auto i : allLvl[2]){\n            cout<<i.first<<" "<<i.second<<endl;\n        }\n\n        return ans;\n    }\n};\n```
['C++']