Tree Traversals ( Inorder , Preorder and Postorder ) | Class containing left and right child of current node and key value ; Root of Binary Tree ; * Given a binary tree , print its nodes according to the " bottom - up " postorder * traversal . ; first recur on left subtree ; then recur on right subtree ; now deal with the node ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Given a binary tree , print its nodes in preorder ; first print data of node ; then recur on left sutree ; now recur on right subtree ; Driver method
class Node { constructor ( val ) { this . key = val ; this . left = null ; this . right = null ; } } var root = null ; function printPostorder ( node ) { if ( node == null ) return ; printPostorder ( node . left ) ; printPostorder ( node . right ) ; document . write ( node . key + " " ) ; } function printInorder ( node ) { if ( node == null ) return ; printInorder ( node . left ) ; document . write ( node . key + " " ) ; printInorder ( node . right ) ; } function printPreorder ( node ) { if ( node == null ) return ; document . write ( node . key + " " ) ; printPreorder ( node . left ) ; printPreorder ( node . right ) ; } 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 ) ; document . write ( " " ) ; printPreorder ( root ) ; document . write ( " " ) ; printInorder ( root ) ; document . write ( " " ) ; printPostorder ( root ) ;