Solve the problem independently in each level of the tree
Intuition - Reduce it to a simpler version you can solve\n Describe your first thoughts on how to solve this problem. \n Find the simplest version of the proble
# Intuition - Reduce it to a simpler version you can solve\n<!-- Describe your first thoughts on how to solve this problem. -->\n* Find the simplest version of the problem that you can solve. \n* Instead of dealing with trees, imaging you had to solve this problem for an array. This is a common technique with tree problems. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n* Make an array corresponding to every level of the tree \n* Solve the problem independently for each level. \n\n# Complexity\n- Time complexity: $$O(N \\log 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[GitHub](https://github.com/MathProgrammer/LeetCode/tree/master/Contests/Weekly%20Contest%20319)\n\n# Code\n```\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    \n    int steps_for(vector <int> &A)\n    {\n        vector <int> sorted_A = A; \n        sort(sorted_A.begin(), sorted_A.end());\n        \n        map <int, int> position;\n        for(int i = 0; i < A.size(); i++)\n        {\n            position[A[i]] = i;\n        }\n        \n        int steps = 0;\n        for(int i = 0; i < A.size(); i++)\n        {\n            if(A[i] == sorted_A[i])\n            {\n                continue;\n            }\n            \n            int p = position[sorted_A[i]];\n            position[sorted_A[i]] = i;\n            position[A[i]] = p;\n            swap(A[i], A[p]);\n            \n            steps++;\n        }\n        return steps;\n    }\n    \n    int minimumOperations(TreeNode* root) {\n        queue <TreeNode*> Q; \n        map <TreeNode*, int> node_level;\n        vector <vector <int> > level; \n        Q.push(root);\n        node_level[root] = 0;\n        \n        while(!Q.empty())\n        {\n            TreeNode* current = Q.front();\n            Q.pop();\n            \n            if(node_level[current] >= level.size())\n            {\n                vector <int> new_level;\n                level.push_back(new_level);\n            }\n            \n            level[node_level[current]].push_back(current->val);\n            \n            if(current->left != NULL)\n            {\n                node_level[current->left] = node_level[current] + 1;\n                Q.push(current->left);\n            }\n            \n            if(current->right != NULL)\n            {\n                node_level[current->right] = node_level[current] + 1;\n                Q.push(current->right);\n            }\n        }\n        \n        int operations = 0;\n        for(int i = 1; i < level.size() ; i++)\n        {\n            operations += steps_for(level[i]);\n        }\n        \n        return operations;\n    }\n};\n```
['C++']