Sobes.tech
Junior — Senior

Mirror reflection of a binary tree

livecode

Task condition

To the given root of the tree root, perform its mirror reflection (invert it) and return the resulting root.

function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val)
    this.left = (left === undefined ? null : left)
    this.right = (right === undefined ? null : right)
}

Examples:

      1           1
     / \         / \
    2   3  =>   3   2
        1           1
       / \         / \
      2   3  =>   3   2
     / \ / \     / \ / \
    4  5 6 ?    ? 6 5  4