Copied to Clipboard
Remove Half Nodes
Easy Accuracy: 51.47% Submissions: 15612 Points: 2

Given A binary Tree. Your task is to remove all the half nodes (which has only one child).

Example 1:

       /   \
      7     8
Output: 2 7 8 

Example 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).

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.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Remove Half Nodes

Output Window