BFS + TreeMap || Detailed Explaination || code--[Java]
IntuitionThe goal of the problem is to find the minimum number of operations (swaps) needed to sort the values of the nodes in a binary tree level by level. Eac
# Intuition
The goal of the problem is to find the minimum number of operations (swaps) needed to sort the values of the nodes in a binary tree level by level. Each level of the tree is processed independently.
# Approach
**Level Order Traversal:** To achieve this, we use a queue to perform a level order traversal of the binary tree. This allows us to process all the nodes at the current level before moving on to the next level. We start by adding the root node to the queue.

**Storing Values and Their Indices:** For each level, we collect the values of the nodes in an array. We also keep track of where each value should be in a sorted order using a TreeMap. In this map, we store the value as the key and its index in the array as the value. This helps us quickly find where each value should go when we start sorting.

**Sorting the Values:** After we have collected the values for a level, we need to sort them. We iterate through the array of values and check if each value is in the correct position. If a value is not in the correct position, we use the TreeMap to find the correct index for that value.

**Counting Swaps:** When we find that a value is not in the right place, we swap it with the value that should be in that position. Each time we perform a swap, we increment a counter that keeps track of the total number of swaps we have made.

*After processing all levels of the tree, we return the total number of swaps needed to sort the values at each level.*

# Complexity
- Time complexity: $$O(n * log(n))$$ where `N`=no. of nodes in tree & `logN`=The sorting operation involves finding the correct index for each value using the TreeMap, which takes `O(log n)` time for each lookup.

- Space complexity: $$O(n)$$ $$O(W)$$ where `W`=is the maximum width of the `tree/level`.

# Code
```java []
class Solution {
    public int minimumOperations(TreeNode root) {
        Queue<TreeNode> q=new LinkedList<>();
        q.add(root);
        int swapcount=0;

        while(!q.isEmpty()){
            int n=q.size();
            int[] arr=new int[n];
            TreeMap<Integer,Integer> pmap=new TreeMap<>();
            for(int k=0;k<n;k++){
                TreeNode curr=q.poll();
                pmap.put(curr.val,k);
                arr[k]=curr.val;
                if(curr.left!=null)q.add(curr.left);
                if(curr.right!=null)q.add(curr.right);
            }
            int i=0;
            while(i<arr.length){
                Map.Entry<Integer, Integer> node = pmap.firstEntry();
                int idx=node.getValue();
                int val=node.getKey();

                if(i!=idx){
                    pmap.put(arr[i], idx);
                    int temp=arr[i];
                    arr[i]=arr[idx];
                    arr[idx]=temp;
                    swapcount++;
                }
                pmap.remove(val);
                i++;
            }
        }

        return swapcount;
    }
}
```
['Java']