Skip to content

Instantly share code, notes, and snippets.

@AlexAegis
Created September 23, 2020 13:14
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 AlexAegis/a72d46c96882898941c554bd284e2cb7 to your computer and use it in GitHub Desktop.
Save AlexAegis/a72d46c96882898941c554bd284e2cb7 to your computer and use it in GitHub Desktop.
Inverting a binary tree in one line
export class Node<T> {
public left?: Node<T>;
public right?: Node<T>;
public constructor(public value: T) {}
public invert(): Node<T> {
return ([this.left, this.right] = [this.right?.invert(), this.left?.invert()]) && this;
}
public toString(): string {
return `[${this.left?.toString() ?? '_'},${this.value},${this.right?.toString() ?? '_'}]`;
}
}
const b = new Node(1);
const a = new Node(2);
const d = new Node(3);
const c = new Node(4);
const e = new Node(5);
a.left = b;
a.right = c;
c.left = d;
c.right = e;
/*
A
/ \
B C
/ \
D E
*/
console.log(a.toString()); // [[_,1,_],2,[[_,3,_],4,[_,5,_]]]
a.invert();
console.log(a.toString()); // [[[_,5,_],4,[_,3,_]],2,[_,1,_]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment