Easy Python Solution | Sorting | Dictionary
Complexity

Time complexity: O(N.log(N))


Space complexity: O(N)

Code
# Complexity
- Time complexity: $$ O(N.log (N))$$

- Space complexity: $$ O(N) $$

# Code
```python3 []

class Solution:
    def minimumOperations(self, root: Optional[TreeNode]) -> int:
        
        q = deque()
        q.append(root)
        ans = 0
        d = {}

        # Perform level order traversal of the Tree
        while q:

            level = []

            for i in range(len(q)):

                node = q.popleft()

                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)

                level.append(node.val)

            final = sorted(level)
            d.clear()

            # make a map that stores the right position of each value
            for i in range(len(level)):
                d[final[i]] = i

            # Swap until the position i has the correct value
            for i in range(len(level)):
                if i == d[level[i]]: continue

                while i != d[level[i]]:
                    temp = level[d[level[i]]]
                    level[d[level[i]]] = level[i]
                    level[i] = temp
                    ans+=1
        return ans

```
['Hash Table', 'Tree', 'Breadth-First Search', 'Python3']