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
time, space.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
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
time, space.