Simple Level Order Traversal and Sorting
Intuition\nMy first thought on solving this problem is to leverage a level-order traversal (BFS) of the binary tree. At each level, we can determine the number 
# **Intuition**\nMy first thought on solving this problem is to leverage a level-order traversal (BFS) of the binary tree. At each level, we can determine the number of swaps needed to sort the nodes\' values into a non-decreasing order.\n\n## Approach\n- Level-order Traversal: Use a queue to perform a breadth-first search (BFS) to traverse the tree level by level.\n- Sorting and Swaps Calculation: For each level, extract the values into an array and determine the number of swaps required to sort this array. This can be done by comparing the current array to its sorted version.\n- Total Moves Calculation: Sum the swaps for all levels to get the total number of operations needed to sort the tree level by level.\n\n## Complexity\n\nTime Complexity:\n\nBFS Traversal: O(n), where n is the number of nodes in the tree.\nSorting Each Level: If we assume m as the maximum number of nodes at any level, the complexity for sorting is O(mlogm). Since each node is processed once, the overall complexity becomes O(nlogm).\nSwaps Calculation: This operation is \uD835\uDC42(\uD835\uDC5A2) in the worst case for each level, but typically it\'s more efficient.\nCombined, the overall time complexity is O(nlogm).\n\nSpace Complexity:\n\nThe space used by the queue for BFS is O(n) in the worst case.\nAdditional space for storing the level values and their sorted version is also O(m) for each level.\nOverall space complexity is O(n).\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n\n    public int moves(int[] a,int[] b){\n        int move=0;\n        int j,i;\n        for(i=0;i<a.length;i++){\n            int ele=b[i];\n            if(a[i]==ele){\n                continue;\n            }\n            else{\n                for(j=i;j<a.length;j++){\n                    if(a[j]==ele){\n                        break;\n                    }\n                }\n                a[j]=a[i];\n                move++;\n            }\n        }\n        return move;\n    }\n\n    public int levelOrder(TreeNode root){\n        Queue<TreeNode> q=new LinkedList<>();\n        q.add(root);\n        int ans=0;\n        while(!q.isEmpty()){\n            int n=q.size();\n            int[] arr=new int[n];\n            int[] arr2=new int[n];\n            int index=0;\n            for(int i=0;i<n;i++){\n                TreeNode ele=q.poll();\n                if(ele.left!=null) {\n                    q.add(ele.left);\n                }\n                if(ele.right!=null) q.add(ele.right);\n\n                arr[index]=ele.val;\n                arr2[index++]=ele.val;\n            }\n\n            Arrays.sort(arr2);\n            ans+=moves(arr,arr2);\n\n        }\n        return ans;\n    }\n\n\n    public int minimumOperations(TreeNode root) {\n        return levelOrder(root);\n    }\n}\n```
['Tree', 'Breadth-First Search', 'Sorting', 'Binary Tree', 'Java']