As reported: Adobe Computer Scientist 1, round 2 (2025): cousins in a binary tree — given a binary tree and a pointer to a target node, return all of its cousins. Two-pass and one-pass BFS were discussed; the two-pass version was coded. Also asked at Adobe in 2025 (Computer Scientist 1, Illustrator team, round 2), where a one-pass solution was written.
Given the root of a binary tree and a node target in it, return the values of all cousins of target: the nodes on the same level (same depth) as target whose parent is not target's parent. Return them from left to right; return an empty list if there are none.
Example 1
root = [1,2,3,4,5,6,7], target = node 5
Output: [6,7]
4 is 5's sibling, not a cousin.
Example 2
root = [1,2,3,4,null,null,5], target = node 4
Output: [5]
Example 3
root = [1,2,3,4,5], target = node 3
Output: []
Constraints: 1 ≤ number of nodes ≤ 2·10^4; node values are distinct; target is a node of the tree.
Code runner format: one JSON value per line: root in level order with null for missing children, then the value of target. The harness finds that node and calls findCousins(root, target) with it; it prints the list of values.