InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Remove Half Nodes

Problem Description

Given a binary tree A with N nodes.

You have to remove all the half nodes and return the final binary tree.


  • Half nodes are nodes which have only one child.
  • Leaves should not be touched as they have both children as NULL.

Problem Constraints

1 <= N <= 105

Input Format

First and only argument is an pointer to the root of binary tree A.

Output Format

Return a pointer to the root of the new binary tree.

Example Input

Input 1:

         /   \
        2     3

Input 2:

          /   \
         2     3
        / \     \
       4   5     6

Example Output

Output 1:

         /    \
        4      3

Output 2:

          /   \
         2     6
        / \
4 5

Example Explanation

Explanation 1:

 The only half node present in the tree is 2 so we will remove this node.

Explanation 2:

 The only half node present in the tree is 3 so we will remove this node.

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Remove Half Nodes on Interview Code Editor
Sign Up
to access hints and editorial solutions for Remove Half Nodes


Click here to start solving coding interview questions