Skip to content

Commit

Permalink
Added Tree_Postorder_Traversal implementation in JavaScript. (jainama…
Browse files Browse the repository at this point in the history
…n224#1978)

* Added Tree_Postorder_Traversal in JavaScript.
  • Loading branch information
UddeshJain authored and ashishnagpal2498 committed Mar 12, 2020
1 parent 0f1a0d4 commit 8f101da
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Tree_Postorder_Traversal/Tree_Postorder_Traversal.js
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
*/

0 comments on commit 8f101da

Please sign in to comment.