LeetCode 226 Invert Binary Tree - Implementation

The approach

Recursively swap children at each node.

function invertTree(root): if not root: return null left = invertTree(root.left) right = invertTree(root.right) root.left = right root.right = left return root

O(n)O(n) time, O(h)O(h) space.