Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22787dd

Browse files
committed
update: return pre-order traversal as an array
1 parent 7b65d6a commit 22787dd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

‎src/_DataStructures_/Trees/BST/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ class BinarySearchTree {
2929
}
3030

3131
preorder(root) {
32-
if (root === null) return;
32+
/** returning an array so as to make testing easy */
33+
let arr = [];
34+
if (root === null) return [];
3335
// eslint-disable-next-line no-console
34-
console.log(`${root.value} `);
36+
arr.push(root.value);
3537

36-
this.preorder(root.leftChild);
37-
this.preorder(root.rightChild);
38+
const left = this.preorder(root.leftChild);
39+
arr = [...arr, ...left];
40+
const right = this.preorder(root.rightChild);
41+
arr = [...arr, ...right];
42+
return arr;
3843
}
3944
}
4045

@@ -48,6 +53,7 @@ class BinarySearchTree {
4853

4954
// console.log(bst.root);
5055

51-
// bst.preorder(bst.root);
56+
// const a = bst.preorder(bst.root);
57+
// console.log('arr = ', a);
5258

5359
module.exports = BinarySearchTree;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /