Skip to content

Commit 298bebc

Browse files
authored
Update Invert Binary Tree - Leetcode 226.py
1 parent fa0cc31 commit 298bebc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Invert Binary Tree - Leetcode 226/Invert Binary Tree - Leetcode 226.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# YouTube Solution
12
class Solution:
23
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
34

@@ -13,3 +14,21 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
1314

1415
# Time Complexity: O(n)
1516
# Space Complexity: O(h) { here "h" is the height of the tree }
17+
18+
19+
# Solution for Bootcamp
20+
class Solution:
21+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
22+
def invert(node):
23+
if not node:
24+
return
25+
26+
node.left, node.right = node.right, node.left
27+
invert(node.left)
28+
invert(node.right)
29+
30+
invert(root)
31+
return root
32+
33+
# Time Complexity: O(n)
34+
# Space Complexity: O(h) { here "h" is the height of the tree }

0 commit comments

Comments
 (0)