#C10459. Connect Siblings in Binary Tree
Connect Siblings in Binary Tree
Connect Siblings in Binary Tree
Given a binary tree, your task is to link each node with its immediate right sibling in the same level. After connecting the siblings, output the nodes level by level by traversing the linked sibling pointers. The tree is provided as a level order traversal in which missing nodes are denoted by N.
You are required to implement an algorithm with an expected time complexity of \(O(N)\), where \(N\) is the number of nodes, and an expected auxiliary space of \(O(1)\) (not counting the space for input and output processing).
Note: The input format is a single line containing node values separated by spaces. If a node is missing, it is represented as N. In the output, each level’s nodes (as connected by sibling pointers) should be printed on a separate line, with values separated by a space.
inputFormat
A single line containing the level order traversal of the binary tree. The values are separated by spaces. Use N to indicate a null (missing) node. For an empty tree, the input will be a single N.
outputFormat
Print the tree level by level by following the sibling pointers that were connected. Each level should be printed on a new line and the node values should be separated by a space.
## sample1 2 3 4 5 6 7
1
2 3
4 5 6 7
</p>