C++|Maps|Set|Preorder Traversal
\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0)
```\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 */\nclass Solution {\npublic:\n    map<int,int>mp;\n    map<int,int>height;\n    map<int,set<pair<int,int>>>mp1;\n    int check(TreeNode*root,int count)\n    {\n        if(!root)return 0;\n        mp[root->val] = count;\n        int a = check(root->left,count+1);\n        int b = check(root->right,count+1);\n        int c = 1+max(a,b);\n        int d = count+max(a,b);// This is to find total height of tree because of this node\n        height[root->val] = d;\n        mp1[count].insert({d,root->val});\n        return c;\n    }\n    vector<int> treeQueries(TreeNode* root, vector<int>& queries) {\n        mp.clear();\n        mp1.clear();\n        height.clear();\n        check(root,0);\n        vector<int>ans;\n        for(auto x:queries)\n        {\n            int a = mp[x];\n            if(mp1[a].size()>1)\n            {\n                int x1 = height[x];\n                auto it = mp1[a].lower_bound({x1,-1});\n                pair<int,int>p = *it;\n                mp1[a].erase(it);\n                auto y = *(mp1[a].rbegin());\n                ans.push_back(y.first);\n                mp1[a].insert(p);\n            }\n            else\n            {\n                ans.push_back(a-1);\n            }\n        }\n        return ans;\n    }\n};\n```
['C', 'Ordered Set']