Similar Problems
Similar Problems not available
Minimum Number Of Operations To Sort A Binary Tree By Level - Leetcode Solution
Companies:
LeetCode: Minimum Number Of Operations To Sort A Binary Tree By Level Leetcode Solution
Difficulty: Medium
Topics: tree binary-tree breadth-first-search
Problem Description:
Given a binary tree, you have to sort the tree in such a way that the levels of the tree are sorted in ascending order of their values. You can swap nodes of the tree (with their subtrees) to achieve this.
Return the minimum number of swaps needed to achieve this.
Solution:
The problem can be solved using a breadth-first search approach. We can traverse the tree level by level and store the nodes in a list for each level. These lists will help us in sorting the nodes level by level.
To sort the nodes level by level, we can implement a custom comparator for the lists. The comparator will sort the nodes in ascending order of their values. After sorting the nodes level by level, we can merge these lists to create a new binary tree.
Finally, we need to count the number of swaps required to convert the original binary tree to the new binary tree. We can do this by performing an inorder traversal of both trees at the same time and counting the number of swaps required to make the two trees identical.
Code:
Here is the Python code to solve the problem:
class Solution:
def minSwaps(self, root: TreeNode) -> int:
# 1. Traverse the tree level by level and store nodes in a list for each level
levels = []
queue = [(root, 0)]
while queue:
node, level = queue.pop(0)
if len(levels) < level + 1:
levels.append([])
levels[level].append(node)
if node.left:
queue.append((node.left, level + 1))
if node.right:
queue.append((node.right, level + 1))
# 2. Sort nodes level by level
for level in levels:
level.sort(key=lambda x: x.val)
# 3. Merge sorted lists to create a new binary tree
new_root = TreeNode(levels[0][0].val)
queue = [(new_root, levels[0][0])]
for i in range(1, len(levels)):
parent, node = queue.pop(0)
if levels[i]:
left = TreeNode(levels[i][0].val)
parent.left = left
queue.append((left, levels[i][0]))
if len(levels[i]) > 1:
right = TreeNode(levels[i][1].val)
parent.right = right
queue.append((right, levels[i][1]))
# 4. Count the number of swaps required to convert the original binary tree to the new tree
count = self.countSwaps(root, new_root)
return count
def countSwaps(self, node1, node2):
if not node1 or not node2:
return 0
count = self.countSwaps(node1.left, node2.left)
if node1.val != node2.val:
count += 1
count += self.countSwaps(node1.right, node2.right)
return count
Time Complexity:
The time complexity of the algorithm is O(n log n), where n is the number of nodes in the binary tree. The traversal of the tree takes O(n) time, while the sorting of each level takes O(log n) time due to the binary tree's inherent structure. Finally, the counting of swaps takes O(n) time. So the overall time complexity is O(n log n).
Minimum Number Of Operations To Sort A Binary Tree By Level Solution Code
1