mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2025-01-03 23:29:46 +08:00
41 lines
713 B
JavaScript
41 lines
713 B
JavaScript
export default class BinaryTreeNode {
|
|
constructor(value = null, left = null, right = null) {
|
|
this.left = left;
|
|
this.right = right;
|
|
this.value = value;
|
|
}
|
|
|
|
addLeft(node) {
|
|
this.left = node;
|
|
return this;
|
|
}
|
|
|
|
addRight(node) {
|
|
this.right = node;
|
|
return this;
|
|
}
|
|
|
|
traverseInOrder() {
|
|
let traverse = [];
|
|
|
|
// Add left node.
|
|
if (this.left) {
|
|
traverse = traverse.concat(this.left.traverseInOrder());
|
|
}
|
|
|
|
// Add root.
|
|
traverse.push(this.value);
|
|
|
|
// Add right node.
|
|
if (this.right) {
|
|
traverse = traverse.concat(this.right.traverseInOrder());
|
|
}
|
|
|
|
return traverse;
|
|
}
|
|
|
|
toString() {
|
|
return this.traverseInOrder().toString();
|
|
}
|
|
}
|