Given a binary tree , how do you remove all the half nodes ? | A 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 None 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 : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printInorder ( root ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT printInorder ( root . left ) NEW_LINE print root . data , NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT DEDENT def RemoveHalfNodes ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = RemoveHalfNodes ( root . left ) NEW_LINE root . right = RemoveHalfNodes ( root . right ) NEW_LINE if root . left is None and root . right is None : NEW_LINE INDENT return root NEW_LINE DEDENT if root . left is None : NEW_LINE INDENT new_root = root . right NEW_LINE temp = root NEW_LINE root = None NEW_LINE del ( temp ) NEW_LINE return new_root NEW_LINE DEDENT if root . right is None : NEW_LINE INDENT new_root = root . left NEW_LINE temp = root NEW_LINE root = None NEW_LINE del ( temp ) NEW_LINE return new_root NEW_LINE DEDENT return root NEW_LINE DEDENT root = Node ( 2 ) NEW_LINE root . left = Node ( 7 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . left . right . left = Node ( 1 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 4 ) NEW_LINE print " Inorder ▁ traversal ▁ of ▁ given ▁ tree " NEW_LINE printInorder ( root ) NEW_LINE NewRoot = RemoveHalfNodes ( root ) NEW_LINE print   " NEW_LINE Inorder traversal of the modified tree " NEW_LINE printInorder ( NewRoot ) NEW_LINE