
g0201_0300.s0226_invert_binary_tree.Solution.py Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
# #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
# #Big_O_Time_O(n)_Space_O(n) #2024_06_09_Time_30_ms_(87.85%)_Space_16.5_MB_(77.12%)
# 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy