Skip to content

Commit 5fd958e

Browse files
committed
update: added Postorder traversal
1 parent 78cb213 commit 5fd958e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/_DataStructures_/Trees/BST/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class BinarySearchTree {
4141
}
4242

4343
inorder(root) {
44+
/** left - root - right */
4445
if (root === null) return [];
4546
let arr = [];
4647
const left = this.inorder(root.leftChild);
@@ -53,6 +54,21 @@ class BinarySearchTree {
5354
arr = [...arr, ...right];
5455
return arr;
5556
}
57+
58+
postorder(root) {
59+
/** left - right - root */
60+
61+
if (root === null) return [];
62+
let arr = [];
63+
64+
const left = this.postorder(root.leftChild);
65+
arr = [...left, ...arr];
66+
67+
const right = this.postorder(root.rightChild);
68+
arr = [...arr, ...right];
69+
70+
return [...arr, root.value];
71+
}
5672
}
5773

5874
// const bst = new BinarySearchTree(6);
@@ -72,4 +88,7 @@ class BinarySearchTree {
7288
// const inorder = bst.inorder(bst.root);
7389
// console.log('Inorder Traversal - ', inorder);
7490

91+
// const postorder = bst.postorder(bst.root);
92+
// console.log('Postorder Traversal - ', postorder);
93+
7594
module.exports = BinarySearchTree;

0 commit comments

Comments
 (0)