Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Vertical Order Traversal of a Binary Tree

XPChallenge Points: 30
levelLevel: Hard

You are given the root of a binary tree. Each node in the tree can be represented by a coordinate (row, col) where the root starts at (0, 0).
For any node located at (row, col):

  • Its left child will be at (row + 1, col - 1)

  • Its right child will be at (row + 1, col + 1)

Your task is to group nodes by their column index, starting from the leftmost column to the rightmost.
Within the same column:

  • Nodes closer to the top (smaller row) should appear first.

  • If multiple nodes share the same (row, col), sort them in increasing order of their values.

Return the final list representing the vertical order traversal of the tree.

Example 1:

Input: root = [2,1,4,null,null,3,5]

Output: [[1],[2,3],[4],[5]]

Example 2:

Input: root = [10,5,15,3,7,null,18]

Output: [[3],[5],[10,7],[15],[18]]

Example 3:

Input: root = [4,2,6,1,3,5,7]

Output: [[1],[2],[4,3,5],[6],[7]]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result