Print alternate nodes from all levels of a Binary Tree | Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree
class Node { constructor ( val ) { this . left = null ; this . right = null ; this . data = val ; } } function PrintAlternate ( root ) { let Q = [ ] ; Q . push ( root ) ; while ( Q . length > 0 ) { let N = Q . length ; for ( let i = 0 ; i < N ; i ++ ) { let temp = Q [ 0 ] ; Q . shift ( ) ; if ( i % 2 == 0 ) { document . write ( temp . data + " " ) ; } if ( temp . left != null ) { Q . push ( temp . left ) ; } if ( temp . right != null ) { Q . push ( temp . right ) ; } } document . write ( " " ) ; } } let root ; root = new Node ( 71 ) ; root . left = new Node ( 88 ) ; root . right = new Node ( 99 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; PrintAlternate ( root ) ;