Easy and best solution beats 98%  using BFS 
lets dry run through 7 6 8 5
in sorted array its positions are
5 6 7 8
0 1 2 3
create a hashMap of 5-0, 6-1, 7-2, 8-3
Now while level[i]!=sort_t[i]:

 level[i]

- lets dry run through 7 6 8 5
-  in sorted array its positions are 
        5 6 7 8
        0 1 2 3
- create a hashMap of 5-0, 6-1, 7-2, 8-3
- Now while level[i]!=sort_t[i]:
-      level[i]=7 and map[7]=2 so swap(7, element at index 2)
-      now element at index 2 is 8 level[i]=8!=5(sort_t[i]) // so loop continues
-      map[8]=3 swap(8,level[3]) level[3]=5 , 5 has come to correct position , hence the loop stops 
- numbers of swaps is 2 , which is number of times the while loop has executed 
```
# Dry run

# swap 7 with element at index 2
7 6 8 5  -->level    
2 1 3 0. -->correct positions in the map

# swap 8 with element at index 3
8 6 7 5      
3 1 2 0

5 6 7 8    
0 1 2 3 
```

# Code
```python3 []
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque
class Solution:
    def minimumOperations(self, root: Optional[TreeNode]) -> int:

        q=deque()
        q.append(root)
        count=0
        while q:
            n=len(q)
            arr_level=[]
            for _ in range(n):
                curr=q.popleft()
                arr_level.append(curr.val)
                if curr.left:
                    q.append(curr.left)
                if curr.right:
                    q.append(curr.right)
 
            sort_t = sorted(arr_level)
            maps = {v: i for i, v in enumerate(sort_t)}
            vis = set()

            for i in range(len(arr_level)):
                while arr_level[i]!=sort_t[i]:
                    count+=1
                    id=maps[arr_level[i]]
                    temp=arr_level[i]
                    arr_level[i]=arr_level[id]
                    arr_level[id]=temp

        return count

```
                    
If you understood the solution , give it an upvote plz 🥺



            
                
            
                
            


            






        

['Breadth-First Search', 'Ordered Map', 'Python3']