Skip to content

Instantly share code, notes, and snippets.

@hk-skit
Created January 27, 2019 12:29
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 hk-skit/67c5b59acabb1052549c601c80d282b3 to your computer and use it in GitHub Desktop.
Save hk-skit/67c5b59acabb1052549c601c80d282b3 to your computer and use it in GitHub Desktop.
export class BinaryTreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
/**
* Inserts node in level order.
*
* @param {*} x
* @returns
* @memberof BinaryTreeNode
*/
add(x) {
const queue = [this];
const newNode = new BinaryTreeNode(x);
while (true) {
const node = queue.shift();
if (node.left === null) {
node.left = newNode;
return this;
}
if (node.right === null) {
node.right = newNode;
return this;
}
queue.push(node.left);
queue.push(node.right);
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment