Search⌘ K
AI Features

Solution: Vertical Order Traversal of a Binary Tree

Solution: Vertical Order Traversal of a Binary Tree groups node values by column, from left to right, ordering each column top to bottom and resolving ties from left to right.

Statement

Given the root of a binary tree, return the vertical order traversal of its nodes' values.

The nodes should be grouped into columns based on their horizontal position in the tree:

  • The root is at column 0.

  • The left child of a node is one column to the left of its parent.

  • The right child of a node is one column to the right of its parent.

  • Return the columns from the leftmost column to the rightmost column.

  • Within each column, return nodes from top to bottom.

  • If two nodes have the same row and column, they should appear from left to right.

Constraints:

  • The number of nodes in the tree is in the range [0, 100].

  • −100≤-100 ≤ Node.val ≤100≤ 100 ...