forked from LeetCode-in-Python/LeetCode-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution0226.py
More file actions
30 lines (25 loc) · 1000 Bytes
/
Solution0226.py
File metadata and controls
30 lines (25 loc) · 1000 Bytes
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
# #Easy #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
# #Data_Structure_I_Day_12_Tree #Level_2_Day_6_Tree #Udemy_Tree_Stack_Queue
# #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(n)_Space_O(n)
# #2025_07_25_Time_0_ms_(100.00%)_Space_17.94_MB_(9.36%)
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# 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]:
if root is None:
return None
# Swap the left and right children
temp = root.left
root.left = self.invertTree(root.right)
root.right = self.invertTree(temp)
return root