Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 636 Bytes

File metadata and controls

45 lines (33 loc) · 636 Bytes

Invert Binary Tree

After reading this tweet, I thought I'd actually suss out the question.

Invert this:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1

Solution:

var invertTree = function(root) {
    if (!root) {
        return null;
    }

    invertTree(root.left);
    invertTree(root.right);

    var temp = root.left;
    root.left = root.right;
    root.right = temp;
  
    return root;

};

The questions/test case can be found here.