DFS IN and OUT timers | Binary Search | C++ | Comments
```\nusing pii = pair;\n\nclass Solution {\npublic:\n    // in out time pairs at particular depth\n    vector g[100005];\n    \n    int in[100005];\n    int out
```\nusing pii = pair<int,int>;\n\nclass Solution {\npublic:\n    // in out time pairs at particular depth\n    vector<pii> g[100005];\n    \n    int in[100005];\n    int out[100005];\n    int timer = 0;\n    \n    // Max Depth Overall\n    int mx;\n\n    \n    // Calculating in out times for each vertex\n    // postorder assignment\n    void dfs(TreeNode* root, int depth)\n    {\n        if(root == nullptr)return;\n        \n        in[root -> val] = timer++;\n\n        dfs(root -> left,depth + 1);\n        dfs(root -> right, depth + 1);\n\n        out[root -> val] = timer++;\n\n        g[depth].push_back({in[root -> val], out[root -> val]});\n        mx = max(mx,depth);\n    }\n\n    // check for particular height\n    int check(int mid, int src)\n    {\n        if(g[mid].size() == 0) return 0;\n\n        // check if first vertex don\'t lie in subtree of src\n        if(!(g[mid][0].first >= in[src] && g[mid][0].second <= out[src])) return 1;\n\n        // check if last vertex don\'t lies in subtree of src\n\t\t// Checking first and last is sufficient becase other middle vartices are in range of timings\n        if(!(g[mid].back().first >= in[src] && g[mid].back().second <= out[src])) return 1;\n\n\n        return 0;\n    }\n\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n\n        for(int i=0;i;i++)\n        {\n            if(g[i].size() == 0)break;\n            g[i].clear();\n        }\n\n        // resetting \n        timer = 0;\n        mx = 0;\n        dfs(root,0);\n        \n        \n        vector<int> ans;\n\n        for(int q:queries)\n        {\n            int lo = 0;\n            int hi = mx;\n            int h = 1;\n\n            while(lo <= hi)\n            {\n                int mid = (lo + hi) / 2;\n\n                if(check(mid,q))\n                {\n                    h = mid;\n                    lo = mid + 1;\n                }\n                else\n                {\n                    hi = mid - 1;\n                }\n            }\n\n            ans.push_back(h);\n        }\n\n        return ans;\n    }\n};
['Tree', 'Depth-First Search', 'Graph', 'Binary Tree']