Given A binary Tree. Your task is to remove all the half nodes (which has only one child).
Input: 7 / \ 7 8 / 2 Output: 2 7 8
Input: 2 / \ 7 5 \ \ 9 1 / \ 11 4 Output: 1 6 11 2 4
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.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?Yes