✅ 🌟 JAVA  SOLUTION ||🔥 BEATS 18% PROOF🔥|| 💡 CONCISE CODE ✅ || 🧑‍💻 BEGINNER FREINDLY
Approach1. Level Order Traversal (BFS):
Use a queue to traverse the binary tree level by level.
For each level, collect node values and their indices.
2. Sort w
# Approach
<!-- Describe your approach to solving the problem. -->
# 1. Level Order Traversal (BFS):
- Use a queue to traverse the binary tree level by level.
- For each level, collect node values and their indices.
# 2. Sort with Index Mapping:
- Pair each value with its original index.
- Sort the values, preserving their original indices.
# 3. Count Minimum Swaps:
- Determine the number of swaps needed to rearrange the values to match their sorted order. This involves cycling through misplaced elements and swapping them to their correct positions.
# 4. Repeat for All Levels:
- Process all levels independently to calculate the total number of swaps.

---

# Complexity
- Time complexity:O(nlogn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int minimumOperations(TreeNode root) {
        Queue<TreeNode> q=new LinkedList<>();
        q.add(root);
        int count=0;
        while(!q.isEmpty())
        {
            TreeNode curr=q.peek();
            int size=q.size();
            for(int i=0;i<size;i++)
            {
                curr=q.remove();
                if(curr.left!=null)
                {
                    q.add(curr.left);
                }
                if(curr.right!=null)
                {
                    q.add(curr.right);
                }
            }
            int[] arr=new int[q.size()];
            int j=0;
            for(TreeNode ele:q)
            {
                arr[j++]=ele.val;
            }
            count+=minSwaps(arr, j);
        }
        return count;
    }
    private static int minSwaps(int []arr, int n)
    {
        int ans=0;
        int []swaps=Arrays.copyOfRange(arr, 0, n);
        Arrays.sort(swaps);
        for(int i=0;i<n;i++)
        {
            if(arr[i]!=swaps[i])
            {
                ans++;
                swap(arr, i, indexOf(arr, swaps[i]));
            }
        }
        return ans;
    }
    private static void swap(int []arr, int i, int j)
    {
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    private static int indexOf(int []arr, int num)
    {
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i]==num)
            {
                return i;
            }
        }
        return -1;
    }
}
```
['Tree', 'Breadth-First Search', 'Binary Tree', 'Java']