forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Tree_Postorder_Traversal implementation in JavaScript. (jainama…
…n224#1978) * Added Tree_Postorder_Traversal in JavaScript.
- Loading branch information
1 parent
ac0ae30
commit f95e471
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Program to traverse a tree in Post Order. | ||
*/ | ||
|
||
// Creates a Tree Node with input value. | ||
class Node { | ||
constructor(value) { | ||
this.value = value; | ||
this.left = null; | ||
this.right = null; | ||
} | ||
} | ||
|
||
// Function for POST ORDER Tree Transversal. | ||
function PostOrder(root) { | ||
if (root) { | ||
PostOrder(root.left); | ||
PostOrder(root.right); | ||
console.log(root.value); | ||
} | ||
} | ||
|
||
// Sample Input. | ||
var root = new Node(1); | ||
root.left = new Node(2); | ||
root.right = new Node(3); | ||
root.left.left = new Node(4); | ||
root.left.right = new Node(5); | ||
root.right.left = new Node(6); | ||
root.right.right = new Node(7); | ||
|
||
// Sample Output | ||
console.log("Post Order traversal of tree is:-"); | ||
PostOrder(root); | ||
|
||
/* | ||
* Expected Output:- 4 5 2 6 7 3 1 | ||
*/ |