Categories
interview

Invert Binary Tree

Javascript

/**
 * Definition for a binary tree node.
 */
 class TreeNode {
  constructor(val, left, right) {
    this.val = (val === undefined ? 0 : val)
    this.left = (left === undefined ? null : left)
    this.right = (right === undefined ? null : right)
  }
}

/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
const invertTree = root => root !== null ? new TreeNode(root.val, invertTree(root.right), invertTree(root.left)) : null;