-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path175.py
40 lines (35 loc) · 994 Bytes
/
175.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
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: a TreeNode, the root of the binary tree
@return: nothing
"""
def invertBinaryTree(self, root):
# write your code here
if not root:
return
tmp = root
stack = []
stack.append(tmp)
while stack:
item = stack.pop()
item.right, item.left = item.left, item.right
if item.left:
stack.append(item.left)
if item.right:
stack.append(item.right)
return
# def invertBinaryTree(self, root):
# # write your code here
# if not root:
# return
# root.left, root.right = root.right, root.left
# self.invertBinaryTree(root.left)
# self.invertBinaryTree(root.right)
# return