Invert a Binary Tree

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write invertTree(root). A binary tree is made of nodes { value, left, right }, where left and right are child nodes or null; an empty tree is null. Mirror the tree in place by swapping the left and right children of every node, and return the same root, or null for an empty tree. For example, the tree written in level order as [4, 2, 7, 1, 3, 6, 9], with null for a missing child, becomes [4, 7, 2, 9, 6, 3, 1]: 7 is now the left child of 4, and 9 the left child of 7.

Your code must pass

  • Mirrors a full tree
  • Mirrors an uneven tree
  • Handles an empty tree and a single node
  • Changes the tree in place and returns its root