-
Notifications
You must be signed in to change notification settings - Fork 0
/
python226.py
41 lines (31 loc) · 1021 Bytes
/
python226.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
37
38
39
40
41
# 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]:
return self.invert(root)
# newTree = TreeNode(0)
# node = root
# new_node = newTree
# new_node.val = node.val
# while True:
# if node:
# new_node.left = node.right
# new_node.right = node.left
# node = node.left
# new_node = new_node.right
# else:
# break
# return newTree
### Recursive
def invert(self, node):
if node:
tmp = node.left
node.left = node.right
node.right = tmp
self.invert(node.left)
self.invert(node.right)
return node