247. Populating Next Right Pointers in Each Node
You are given a perfect binary tree where:
- All leaves are on the same level, and
- Every parent has exactly two children.
The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
You need to populate each next pointer so that it points to its next right node.
If there is no next right node, the next pointer should be 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's `next` pointer is connected to the node on its right at the same level. '#' indicates the end of each level.

Example 2:
Input: root = []
Output: []
Explanation:
The tree is empty, so there are no nodes to connect.
Now Your Turn!
Pick the correct output for the given inputInput: root = [10,20,30,40,50,60,70]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- The number of nodes in the tree is in the range [0, 212 - 1]
- -1000 <= Node.val <= 1000