Remove Half Nodes | Practice | GeeksforGeeks

Given A binary Tree. Your task is to remove all the half nodes (which has only one child).
Example 1:
Input:
         7
       /   \
      7     8
     / 
    2
Output: 2 7 8 

Example 2:
Input:
         2
       /   \
      7     5
       \      \
        9      1
       /  \
      11   4
Output: 11 9 4 2 1 
Your Task:
You don't need to read input or print anything. Your task is to complete the function RemoveHalfNodes() which takes the root node of the tree as input and returns the root node of the modified tree after removing all the half nodes, ie the ones having just a single child node. (The inorder traversal of the returned tree is printed by the driver's code.)
For example consider the below tree.

Nodes 7, 5 and 9 are half nodes as one of their child is Null. We need to remove all such half nodes and return the root pointer of following new tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Binary Tree).
Constraints:
1<=Number of nodes<=104
Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
vrajeshmodi991 month ago
Accuracy 100%
Time Taken 0.2/1.6
void helper(Node*root,Node*prev,string dir){
    if(!root)return;
    if(!root->left && !root->right)return;
    if(!root->left){
        if(dir == "left"){
            prev->left = root->right;
            helper(root->right,prev,"left");
        }
        else if(dir=="right"){
             prev->right = root->right;
             helper(root->right,prev,"right");
        }
        
    }
    else if(!root->right){
        if(dir == "left"){
            prev->left = root->left;
            helper(root->left,prev,"left");
        }
        else if(dir=="right"){
             prev->right = root->left;
             helper(root->left,prev,"right");
        }
    }
    if(root->left)helper(root->left,root,"left");
    if(root->right)helper(root->right,root,"right");
    
}

Node *RemoveHalfNodes(Node *root)
{
   helper(root->left,root,"left");
   helper(root->right,root,"right");
   return root;
}
0
forcer2 months ago
//C++
Node * solve(Node *root)
{
    if(!root) return root;
     root->left=solve(root->left);
    root->right=solve(root->right);
    if(!root->left&&root->right)
    return root->right;
    if(root->left&&!root->right)
    return root->left;
   
    return root;
}
Node *RemoveHalfNodes(Node *root)
{
   //add code here.
   solve(root);
   return root;
}
+1
dhirunand3 months ago
class Tree
{
    public static Node RemoveHalfNodes(Node root)
    {
        // Code Here
        if(root == null)
            return null;
        
        if(root.left==null && root.right ==null)
            return root;
        
        if(root.left==null && root.right !=null)
            return RemoveHalfNodes(root.right);
        
        if(root.left!=null && root.right ==null)
            return RemoveHalfNodes(root.left);
        
        root.left = RemoveHalfNodes(root.left);
        root.right = RemoveHalfNodes(root.right);
        
        return root;
    }
}
0
chanduchandrasekhar23563 months ago
Node *RemoveHalfNodes(Node *root){  if(!root){      return root;  }  if(root->left==NULL&&root->right==NULL){      return root;  }  root->left=RemoveHalfNodes(root->left);  root->right=RemoveHalfNodes(root->right);    if(root->left==NULL&&root->right!=NULL||root->left!=NULL&&root->right==NULL){      return !root->left?root->right:root->left;  }    return root;}
0
nekhatperveen3 months ago
JAVA
{if (root == null || (root.left==null && root.right==null)) return root;       if (root.left!=null && root.right==null) {           return RemoveHalfNodes(root.left);       }       else if (root.left==null && root.right!=null) {           return RemoveHalfNodes(root.right);       }       else 
     {           Node newLeft = RemoveHalfNodes(root.left);           Node newRight = RemoveHalfNodes(root.right);           root.left = newLeft;           root.right = newRight;           return root;       }
}
0
kake13375 months ago
Node *RemoveHalfNodes(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->left;  if(root->right!=NULL && root->left==NULL)  return root->right;  return root;}
0
kake1337
This comment was deleted.
+1
ariespratyush5 months ago
class Tree
{
   // Return the root of the modified tree after removing all the half nodes.
   public static Node RemoveHalfNodes(Node root)
   {
       if(root==null)
       return root;
       root.left=RemoveHalfNodes(root.left);
       root.right=RemoveHalfNodes(root.right);
       if(root.left!=null&&root.right==null)
       return root.left;
       if(root.left==null&&root.right!=null)
       return root.right;
       return root;
   }
}
0
dreamvivek65 months ago
 if(root==NULL) return root;    root->left=RemoveHalfNodes(root->left);  root->right=RemoveHalfNodes(root->right);    if(root->left==NULL && root->right!=NULL) return root->right;   if(root->left!=NULL && root->right==NULL) return root->left; return root;
+2
piyushbisht36 months ago
Checking two cases for each node
-Root left exists , but no root right
-Root right exists ,but no root left
 For either of the above cases we return the corresponding root→left or root→right.
If none of the condition follows, we simply return the root.
 
Node *RemoveHalfNodes(Node *root){   if(root==NULL)       return NULL;   root->left=RemoveHalfNodes(root->left);   root->right=RemoveHalfNodes(root->right);   if(root->left && !root->right)   {       return root->left;   }   else if(!root->left && root->right)   {       return root->right;   }   return root;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.