Given a binary tree , how do you remove all the half nodes ? | Binary tree node ; For inorder traversal ; Removes all nodes with only one child and returns new root ( note that root may change ) ; * if current nodes is a half node with left child NULL left , then it 's right  * child is returned and replaces it in the given tree   ; * if current nodes is a half node with right child NULL right , then it 's right  * child is returned and replaces it in the given tree   ; Driver program
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } var root ; function printInorder ( node ) { if ( node != null ) { printInorder ( node . left ) ; document . write ( node . data + " " ) ; printInorder ( node . right ) ; } } function RemoveHalfNodes ( node ) { if ( node == null ) return null ; node . left = RemoveHalfNodes ( node . left ) ; node . right = RemoveHalfNodes ( node . right ) ; if ( node . left == null && node . right == null ) return node ; if ( node . left == null ) { new_root = node . right ; return new_root ; } if ( node . right == null ) { new_root = node . left ; return new_root ; } return node ; } NewRoot = null ; root = new Node ( 2 ) ; root . left = new Node ( 7 ) ; root . right = new Node ( 5 ) ; root . left . right = new Node ( 6 ) ; root . left . right . left = new Node ( 1 ) ; root . left . right . right = new Node ( 11 ) ; root . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 4 ) ; document . write ( " " ) ; printInorder ( root ) ; NewRoot = RemoveHalfNodes ( root ) ; document . write ( " " ) ; printInorder ( NewRoot ) ; script