Remove all subtrees consisting only of even valued nodes from a Binary Tree | Node of the tree ; Function to create a new node ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer
class node { constructor ( key ) { this . left = null ; this . right = null ; this . data = key ; } } function newNode ( key ) { let temp = new node ( key ) ; return ( temp ) ; } function printLevelOrder ( root ) { if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( q . length != 0 ) { let temp = q [ 0 ] ; document . write ( temp . data + " " ) ; q . shift ( ) ; if ( temp . left != null ) { q . push ( temp . left ) ; } else if ( temp . right != null ) { document . write ( " " ) ; } if ( temp . right != null ) { q . push ( temp . right ) ; } else if ( temp . left != null ) { document . write ( " " ) ; } } } function pruneTree ( root ) { if ( root == null ) { return null ; } root . left = pruneTree ( root . left ) ; root . right = pruneTree ( root . right ) ; if ( root . data % 2 == 0 && root . right == null && root . left == null ) return null ; return root ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 10 ) ; root . right = newNode ( 7 ) ; root . right . left = newNode ( 12 ) ; root . right . right = newNode ( 5 ) ; let newRoot = pruneTree ( root ) ; printLevelOrder ( newRoot ) ;