-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
36 lines (30 loc) · 1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minimumOperations(self, root: Optional[TreeNode]) -> int:
result = 0
q = [root]
while len(q) > 0:
q_new = []
vals = []
for u in q:
if u.left is not None:
q_new.append(u.left)
if u.right is not None:
q_new.append(u.right)
unordered = 0
vals.append(u.val)
idx = {got: i for i, got in enumerate(vals)}
for i, want in enumerate(sorted(vals)):
if want != vals[i]:
j = idx[want]
vals[i], vals[j] = vals[j], vals[i]
idx[vals[i]] = i
idx[vals[j]] = j
result += 1
q = q_new
return result