Loading problem...
You are given the root node of a binary tree structure. Your task is to traverse the tree horizontally, visiting nodes at each depth level from left to right, and return all node values organized by their respective levels.
The traversal should explore all nodes at depth 0 (the root), then all nodes at depth 1, then all nodes at depth 2, and so on. Within each level, nodes should be visited in left-to-right order based on their position in the tree.
Return the values as a nested collection where each inner collection contains all node values at that particular depth level.
root = [3,9,20,null,null,15,7][[3],[9,20],[15,7]]Level 0 contains only the root node with value 3. Level 1 contains nodes 9 (left child) and 20 (right child). Level 2 contains nodes 15 and 7, which are children of node 20. The result groups values by their depth level.
root = [1][[1]]The tree consists of only a single root node with value 1. There is only one level (level 0), so the result contains a single inner list.
root = [][]An empty tree has no nodes to traverse. The result is an empty nested collection indicating no levels exist.
Constraints