You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.
Example 1
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Each node points to the node on its right at the same level: 2 → 3 and 4 → 5 → 6 → 7. The last node on each level points to null, shown as '#'.
Example 2
Input: root = []
Output: []
Explanation: The tree is empty, so there are no pointers to set.
Example 3
Input: root = [1]
Output: [1,#]
Explanation: The root is alone on its level, so its next pointer is null: [1,#].
Process each level left-to-right using BFS, linking each node's next pointer to the next node in the queue at the same level.
Run checks all cases above. Submit evaluates all test cases.