Linked List of Binary Tree Nodes at a Given Level
Problem statement
Collect the binary-tree nodes at zero-based depth level from left to right, as if appending them to a new singly linked list. Return that list's values. Return empty when the level is absent.
Function
nodesAtLevel(root: TreeNode, level: int) → int[]Examples
Example 1
root = [1,2,3,4,5,null,6]level = 2return = [4,5,6]The depth-two nodes appear left-to-right as 4,5,6.
Constraints
- The tree contains at most
10^5nodes. level >= 0.