Skip to content

Commit de0665b

Browse files
committed
Time: 0 ms (100%), Space: 17.9 MB (30.1%) - LeetHub
1 parent 47bd95a commit de0665b

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
class TreeNode:
2-
def __init__(self, val=0, left=None, right=None):
3-
self.val = val
4-
self.left = left
5-
self.right = right
6-
71
class Solution:
82
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9-
if not root:
10-
return None
11-
3+
if not root: #Base Case
4+
return root
5+
self.invertTree(root.left) #Call the left substree
6+
self.invertTree(root.right) #Call the right substree
7+
# Swap the nodes
128
root.left, root.right = root.right, root.left
13-
14-
self.invertTree(root.left)
15-
self.invertTree(root.right)
16-
17-
return root
9+
return root # Return the root

0 commit comments

Comments
 (0)