Given the root of a binary tree, invert the tree, and return its root. Inversion means every node's left and right children are swapped — recursively throughout the entire tree.
root = [4,2,7,1,3,6,9][4,7,2,9,6,3,1]root = [2,1,3][2,3,1]root = [1][1]root == null → return nulltmp=root.left; root.left=root.right; root.right=tmpinvertTree(root.left)invertTree(root.right)rootWe swap a node's children before recursing into them. This is safe because the swap is an O(1) pointer exchange — no recursion needed first. After swapping, we recurse into what is now root.left (which was previously root.right) and vice versa. Every node is visited exactly once → O(n) time, O(h) stack space where h is the tree height.