-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinvt_bin_tree.py
30 lines (26 loc) · 1.01 KB
/
invt_bin_tree.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
# 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
from collections import deque
class Solution:
def __method1 (self, root):
if (root == None): return root
if (root.left != None): self.__method1(root.left)
if (root.right != None): self.__method1(root.right)
root.left, root.right = root.right, root.left
return root
def __method2 (self, root):
if (not root): return root
q = deque() ; q.append(root)
while (q):
cur_root = q.popleft()
if (cur_root.left != None): q.append(cur_root.left)
if (cur_root.right != None): q.append(cur_root.right)
cur_root.left, cur_root.right = cur_root.right, cur_root.left
return root
def invertTree (self, root: Optional[TreeNode]) -> Optional[TreeNode]:
#return self.__method1(root)
return self.__method2(root)