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

0% completed

Connect Level Order Siblings (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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 a null node.

Examples

Example 1:

  • Input: root = [1, 2, 3, 4, 5, 6, 7]
  • Output:
    [1 -> null]
    [2 -> 3 -> null]
    [4 -> 5 -> 6 -> 7 -> null]
    
  • Explanation:
    The tree is traversed level by level using BFS. Each node is connected to its next right node at the same level. The last node of each level points to null.

Example 2:

  • Input: root = [12, 7, 1, 9, null, 10, 5]
  • **Output:

.....

.....

.....

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