text: Given a binary tree , how do you remove all the half nodes ? | C program to remove all 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
code: #include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE struct node { int data ; struct node * left , * right ; } ; struct node * newNode ( int data ) { struct node * node = ( struct node * ) malloc ( sizeof ( struct node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } void printInoder ( struct node * root ) { if ( root != NULL ) { printInoder ( root -> left ) ; printf ( " % d ▁ " , root -> data ) ; printInoder ( root -> right ) ; } } struct node * RemoveHalfNodes ( struct node * root ) { if ( root == NULL ) return NULL ; root -> left = RemoveHalfNodes ( root -> left ) ; root -> right = RemoveHalfNodes ( root -> right ) ; if ( root -> left == NULL && root -> right == NULL ) return root ; if ( root -> left == NULL ) { struct node * new_root = root -> right ; free ( root ) ; return new_root ; } if ( root -> right == NULL ) { struct node * new_root = root -> left ; free ( root ) ; return new_root ; } return root ; } int main ( void ) { struct node * NewRoot = NULL ; struct node * root = newNode ( 2 ) ; root -> left = newNode ( 7 ) ; root -> right = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 1 ) ; root -> left -> right -> right = newNode ( 11 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> left = newNode ( 4 ) ; printf ( " Inorder ▁ traversal ▁ of ▁ given ▁ tree ▁ STRNEWLINE " ) ; printInoder ( root ) ; NewRoot = RemoveHalfNodes ( root ) ; printf ( " Inorder traversal of the modified tree " printInoder ( NewRoot ) ; return 0 ; }