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

0% completed

Solution: Level Order Successor

Problem Statement

Given a binary tree and an integer key, find the level order successor of the node containing the given key as a value in the tree. The level order successor is the node that appears right after the given node in the level order traversal.

Example 1:

Example 2:

Example 3:

Constraints:

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

Solution

This problem follows the Binary Tree Level Order Traversal pattern. We can follow the same BFS approach

.....

.....

.....

Like the course? Get enrolled and start learning!