DFS approach (Slow approach for beginner)
IntuitionApproachComplexity
Time complexity: O(nlogn)

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

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

# 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 {
    List<List<Integer>> lvl;
    public int minimumOperations(TreeNode root) {
        lvl = new ArrayList<>();

        dfs(root, 0);

        int ans = 0;

        for(int i = 1; i < lvl.size(); i++){
            int[] arr = new int[lvl.get(i).size()];
            int k = 0;
            for(int j : lvl.get(i)) arr[k++] = j;

            ans += minSwaps(arr, k);
        }
        return ans;
    }

    private void dfs(TreeNode node, int d){
        if(node == null) return;

        if(lvl.size() == d){
            lvl.add(new ArrayList<>());
        }

        lvl.get(d).add(node.val);

        dfs(node.left, d + 1);
        dfs(node.right, d + 1);

        
    }

    private int indexOf(int[] arr, int ele) {
        for (int i = 0; i < arr.length; i++)
            if (arr[i] == ele)
                return i;

        return -1;
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    private int minSwaps(int[] arr, int N) {
        int ans = 0;
        int[] temp = Arrays.copyOfRange(arr, 0, N);
        Arrays.sort(temp);

        for (int i = 0; i < N; i++) {
            if (arr[i] != temp[i]) {
                ans++;
                swap(arr, i, indexOf(arr, temp[i]));
            }
        }
        return ans;
    }
}
```
['Depth-First Search', 'Sorting', 'Java']