File tree 1 file changed +19
-0
lines changed
src/_DataStructures_/Trees/BST
1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ class BinarySearchTree {
41
41
}
42
42
43
43
inorder ( root ) {
44
+ /** left - root - right */
44
45
if ( root === null ) return [ ] ;
45
46
let arr = [ ] ;
46
47
const left = this . inorder ( root . leftChild ) ;
@@ -53,6 +54,21 @@ class BinarySearchTree {
53
54
arr = [ ...arr , ...right ] ;
54
55
return arr ;
55
56
}
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
+ }
56
72
}
57
73
58
74
// const bst = new BinarySearchTree(6);
@@ -72,4 +88,7 @@ class BinarySearchTree {
72
88
// const inorder = bst.inorder(bst.root);
73
89
// console.log('Inorder Traversal - ', inorder);
74
90
91
+ // const postorder = bst.postorder(bst.root);
92
+ // console.log('Postorder Traversal - ', postorder);
93
+
75
94
module . exports = BinarySearchTree ;
You can’t perform that action at this time.
0 commit comments