Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- link="https://www.codingninjas.com/codestudio/problems/minimum-swaps-to-convert-binary-tree-into-bst_1118109?leftPanelTab=1"
- def minimumSwaps(arr: list, n: int):
- res=[]
- def inorder(i):
- if i>=n:return
- inorder(2*i+1)
- res.append(arr[i])
- inorder(2*i+2)
- inorder(0)
- count=0
- i=0
- items=[(j,i) for i,j in enumerate(res)]
- items.sort()
- while i<n:
- if items[i][1]==i:i+=1
- else:
- y=items[i][1]
- items[i],items[y]=items[y],items[i]
- count+=1
- return count
Advertisement
Add Comment
Please, Sign In to add comment