:
4
/ \
2 7
/ \ / \
1 3 6 9
:
4
/ \
7 2
/ \ / \
9 6 3 1
const tree = {left: {value: 2}, value: 1, right:{value: 4}}
const inverted = invertTree(tree);
{left: {value: 4}, value: 1, right:{value: 2}}
const invertTree = ({ value, left, right }) => {
const node = { value };
if (left) {
node.right = invertTree(left);
}
if (right) {
node.left = invertTree(right);
}
return node;
};