 🔥 BEATS 100% PROOF🔥|| 💡 CONCISE CODE ✅ || 🌟 JAVA || 🧑‍💻 BEGINNER FREINDLY
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

- Space complexity:
<!-- 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) {
        if (root == null) {
            return 0;
        }

        int ans = 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);

        while (!q.isEmpty()) {
            List<Integer> vals = new ArrayList<>();
            List<Integer> ids = new ArrayList<>();
            int sz = q.size();

            // Collect node values and populate the list for the current level
            for (int i = 0; i < sz; i++) {
                TreeNode node = q.poll();
                vals.add(node.val);

                if (node.left != null) {
                    q.offer(node.left);
                }
                if (node.right != null) {
                    q.offer(node.right);
                }
            }

            // Initialize ids for the current level
            for (int i = 0; i < vals.size(); i++) {
                ids.add(i);
            }

            // Sort ids based on the corresponding node values
            Collections.sort(ids, (i, j) -> Integer.compare(vals.get(i), vals.get(j)));

            // Calculate minimum swaps to sort the current level
            for (int i = 0; i < ids.size(); i++) {
                while (ids.get(i) != i) {
                    ans++;
                    swap(ids, i, ids.get(i));
                }
            }
        }

        return ans;
    }

    private void swap(List<Integer> ids, int i, int j) {
        int temp = ids.get(i);
        ids.set(i, ids.get(j));
        ids.set(j, temp);

    }
}
```
['Tree', 'Breadth-First Search', 'Binary Tree', 'Java']