You are given a binary tree consisting of ‘N’ nodes. Your task is to replace all the half nodes present in the binary tree with its child node. Half nodes are those nodes that have only one child.
For Example:
In this given binary tree, nodes 7, 5, and 9 are half nodes because they have only one child node. So we have to replace these nodes with their child. The inorder traversal of the updated binary tree is [1, 6, 11, 2, 4]. Hence, the answer is [1, 6, 11, 2, 4].

Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level.
The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
The first line contains a single integer ‘T’, representing the number of test cases.
The first line of each test case contains space-separated integers representing the level order traversal of the binary tree. If any node does not have a left or right child, take -1 in place of the child.
Output format :
For each test case, print the inorder traversal of the updated tree in a space-separated manner.
Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 5
1 <= ‘N’ <= 10^5
1 <= nodeVal <= 10^12
Time Limit: 1 sec
2
7 12 8 2 -1 -1 -1 -1 -1
1 2 3 -1 4 -1 7 5 6 8 -1 -1 -1 -1 -1 -1 -1
2 7 8
5 4 6 1 8
In test case 1:
In the given binary tree, node 12 is the half node because it has only one child. So we have to replace that node with their child. The inorder traversal of the updated binary tree is [2, 7, 8]. Hence, the answer is [2, 7, 8].
In test case 2:
In the given binary tree, nodes 2, 3, and 7 are the half nodes because they have only one child. So we have to replace these nodes with their child. The inorder traversal of the updated binary tree is [5, 4, 6, 1, 8]. Hence, the answer is [5, 4, 6, 1, 8].
2
1 -1 5 -1 7 -1 9 -1 -1
2 7 5 -1 9 -1 1 11 4 -1 -1 -1 -1 -1 -1
9
11 9 4 2 1
Try to think of a Depth First Search approach.
In this approach, we will traverse the tree in Depth First Search. We will make a recursive function removeHalfNodes(root). The base condition is if the root is an empty node, then we will return an empty node. We will make the recursive call on the left and right children to traverse on the left and right subtree. The conditions to find the half nodes are-:
In the end, we will return the updated root node.
Algorithm:
O(N) where N is the number of nodes in the given binary tree.
We are traversing each node recursively only once, which takes O(N) time. Hence, the overall time complexity is O(N).
O(N) where N is the number of nodes in the given binary tree.
The space complexity O(N) is used for the recursion stack in the worst case. Hence, the overall space complexity is O(N).