Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Connect Level Order Siblings

Problem Statement

Given a binary tree, connect each node with its level order successor. The last node of each level should point to a null node.

Example 1:

Example 2:

Constraints:

  • The number of nodes in the tree is in the range [0, 2<sup>12</sup> - 1].
  • -1000 <= Node.val <= 1000

Solution

This problem follows the Binary Tree Level Order Traversal pattern. We can follow the same BFS approach. The only difference is that while traversing a level we will remember the previous node to connect it with the current node.

.....

.....

.....

Like the course? Get enrolled and start learning!