Skip to content

Instantly share code, notes, and snippets.

@mvasin
Created September 19, 2018 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvasin/78e61fc9356f1ef1cc2214fbdffa9874 to your computer and use it in GitHub Desktop.
Save mvasin/78e61fc9356f1ef1cc2214fbdffa9874 to your computer and use it in GitHub Desktop.
const nullNode = null;
const node1 = {
left: nullNode,
right: nullNode,
value: 1
};
const node3 = {
left: nullNode,
right: nullNode,
value: 3
};
const node6 = {
left: nullNode,
right: nullNode,
value: 6
};
const node9 = {
left: nullNode,
right: nullNode,
value: 9
};
const node2 = {
left: node1,
right: node3,
value: 2
};
const node7 = {
left: node6,
right: node9,
value: 7
};
const node4 = {
left: node2,
right: node7,
value: 4
};
function invertTree(tree) {
// base recursion case
if (!tree.left) return tree;
const reversedLeft = invertTree(tree.left)
const reversedRight = invertTree(tree.right)
const reversedNode = {
left: reversedRight,
right: reversedLeft,
value: tree.value
};
return reversedNode;
}
invertTree(node4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment