-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvert-binary-tree.py3
More file actions
32 lines (26 loc) · 919 Bytes
/
invert-binary-tree.py3
File metadata and controls
32 lines (26 loc) · 919 Bytes
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
# 226. Invert Binary Tree (2/20/56549)
# Runtime: 40 ms (17.13%) Memory: 16.57 MB (0.00%)
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
frontier = deque()
if root:
frontier.append(root)
while frontier:
for _ in range(len(frontier)):
curr = frontier.popleft()
print(curr)
left = curr.left
right = curr.right
if left:
frontier.append(left)
if right:
frontier.append(right)
curr.right = left
curr.left = right
return root