-
Notifications
You must be signed in to change notification settings - Fork 233
/
invert-binary-tree.py
62 lines (50 loc) · 1.36 KB
/
invert-binary-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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# INVERT BINARY TREE
# O(N) time and O(d) space
def invertBinaryTree(tree):
# Write your code here.
if tree is None:
return
tempLeft = tree.left
tree.left = invertBinaryTree(tree.right)
tree.right = invertBinaryTree(tempLeft)
return tree
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# O(N) time and O(d) space
""" def invertBinaryTree(tree):
# Write your code here.
if tree is None:
return
tempLeft = tree.left
tree.left = tree.right
tree.right = tempLeft
invertBinaryTree(tree.left)
invertBinaryTree(tree.right)
return tree
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None """
# O(N) time and space
""" def invertBinaryTree(tree):
# Write your code here.
queue = [tree]
while len(queue) > 0:
currentNode = queue.pop()
if currentNode is None:
continue
currentNode.left, currentNode.right = currentNode.right, currentNode.left
queue.append(currentNode.left)
queue.append(currentNode.right)
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None """