Populating Next Right Pointers in Each Node
You are given a perfect binary tree where every internal node has exactly two children and all leaves are at the same level.
Each node contains an additional next pointer.
Set the next pointer of every node to its next right node at the same level.
If there is no next right node, set next = NULL.
Example
Before:
1
/ \
2 3
/ \ / \
4 5 6 7
After connecting next pointers:
1 → NULL
/ \
2 → 3 → NULL
/ \ / \
4→5→6→7→NULL
Example 1
Input
root = [1,2,3,4,5,6,7]
Output
[1,#,2,3,#,4,5,6,7,#]
Explanation
The tree is:
1
/ \
2 3
/ \ / \
4 5 6 7
At level 1:
1 → NULL
At level 2:
2 → 3 → NULL
At level 3:
4 → 5 → 6 → 7 → NULL
So the next pointers connect every node to the node immediately to its right on the same level.
Example 2
Input
root = []
Output
[]
Explanation
The tree is empty, so there are no nodes to connect.
Constraints
Hints:
Hint 1
Nodes on the same level need to be connected from left to right.
Hint 2
For every parent:
left child → right child
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 12, 2026
Last updated
Aug 13, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output