Skip to content

Commit 580e3ba

Browse files
committed
fix: empty array throw error
1 parent 599263d commit 580e3ba

File tree

1 file changed

+2
-2
lines changed
  • src/_DataStructures_/Trees/BinaryTree

1 file changed

+2
-2
lines changed

src/_DataStructures_/Trees/BinaryTree/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const Node = require('./Node');
22

33
class BinaryTree {
44
constructor(arr) {
5-
if (!Array.isArray(arr)) {
5+
if (!Array.isArray(arr) || !arr.length) {
66
throw new Error('Invalid argument to create a Binary Tre');
77
}
8-
this.root = this.createBinaryTree(this.root, arr, 0);
8+
this.root = this.createBinaryTree((this.root = null), arr, 0);
99
}
1010

1111
// eslint-disable-next-line class-methods-use-this

0 commit comments

Comments
 (0)