Skip to main content

0226 Invert Binary Tree

Solved at: 2022-07-26

Questionโ€‹

Given the root of a binary tree, invert the tree, and return its root.

Solutionโ€‹

# 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 not root:
return
root.left, root.right = root.right, root.left
if root.left:
self.invertTree(root.left)
if root.right:
self.invertTree(root.right)
return root

That was simple...

Resultsโ€‹

Runtimeโ€‹

  • 30 ms, faster than 95.71% of Python3 online submissions for Invert Binary Tree.

Memory Usageโ€‹

  • 13.9 MB, less than 11.66% of Python3 online submissions for Invert Binary Tree.

Other Answers Onlineโ€‹