Grokking Tree Coding Patterns for Interviews

0% completed

Solution: Level Order Successor

Problem Statement

Given a root of the 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.

Examples

Example 1

  • Input: root = [1, 2, 3, 4, 5], key = 3
  • Output: 4
  • Explanation: The level-order traversal of the tree is [1, 2, 3, 4, 5]. The successor of 3 in this order is 4.

Example 2

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

.....

.....

.....

Like the course? Get enrolled and start learning!
M

Mikhail Putilov

· 4 years ago

elegant solution, 47

G

grokking

· 2 years ago

Question says key is a node whereas in reality it is an int. Either pass in the node as stated in the problem description or change the wording to clarify that. Thank you.

Show 1 reply
Spencer Lan

Spencer Lan

· 10 months ago

I'm getting this error even with correct solution, seems like the tree might not be built out correctly for the tests

Show 2 replies