Skip to content

Commit

Permalink
Added Tree Inorder Traversal in JS (jainaman224#2586)
Browse files Browse the repository at this point in the history
* Create Tree_Inorder_Traversal.js

* Update Tree_Inorder_Traversal.js
  • Loading branch information
Aman-Codes authored and afroz23 committed Apr 25, 2020
1 parent 225ddd0 commit ed8d9ec
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Tree_Inorder_Traversal/Tree_Inorder_Traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Program to traverse a tree in Inorder.
*/

// Creates a Tree Node with input value
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}

// Function for In Order Tree Transversal
function InOrder(root) {
if (root) {
InOrder(root.left);
console.log(root.value);
InOrder(root.right);
}
}

// 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("In Order traversal of tree is:-");
InOrder(root);

/* Output
In Order traversal of tree is:- 4 2 5 1 6 3 7
*/

0 comments on commit ed8d9ec

Please sign in to comment.