Back to course home
0% completed
Vote For New Content
Problem Challenge 1: Connect All Level Order Siblings (medium)
Problem Statement
Given a root
of the binary tree, connect each node with its level order successor. The last node of each level should point to the first node of the next level.
Examples
Example 1
- Input: root = [1, 2, 3, 4, 5, 6, 7]
- Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
- Explanation: The tree is traversed level by level using BFS. Each node is connected to its next node in level order traversal, including connections between levels. The last node (
7
) points tonull
.
Example 2
- Input: root = [12, 7, 1, 9, null, 10, 5]
- Output: 12 -> 7 -> 1 -> 9 -> 10 -> 5 -> null
- Explanation: Each node is connected to its next node in level order traversal. The last node (
5
) points tonull
, completing the connection of all level order siblings.
Constraints:
- The number of nodes in the tree is in the range [0, 2<sup>12</sup> - 1].
-1000 <= Node.val <= 1000
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible