This problem follows the Recursive Swap pattern, commonly found in the Trees category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Recursively swap left and right children of every node.
Swap first, then recurse — or recurse first, then swap. Both work because every node gets visited and swapped exactly once.
def invertTree(root):
if not root: return null
root.left, root.right = root.right, root.left
invertTree(root.left)
invertTree(root.right)
return rootPractice Invert Binary Tree and similar Trees problems with flashcards. Build pattern recognition through active recall.
Practice this problem