I knew the definition of cousins (same depth, different parent) but translating that into clean code took me longer than it should have.
Use BFS to traverse the tree level by level, tracking each node's parent. When the target node is found, record its parent and depth, then collect all nodes at the same depth whose parent is different from the target's parent.
Pro tip: Clarify edge cases upfront, such as the target being the root (no cousins) or multiple nodes with the same value. Also, discuss time and space complexity: O(N) time and O(N) space in the worst case.
Confirm the definition of cousin nodes: nodes at the same depth as the target but with different parents. Ask about edge cases like root node, duplicate values, and tree size.
Explain that BFS is ideal for level-order traversal. During BFS, maintain a queue of (node, parent) pairs to easily identify parents when processing each level.
Traverse level by level. When the target node is found, record its parent and the current depth. Continue processing the current level to collect all nodes at that depth.
While processing the target's level, add all nodes whose parent is not the target's parent to the result list. Return the list after finishing the level.
State that time complexity is O(N) since each node is visited once, and space complexity is O(N) for the queue. Mention handling of empty tree, target not found, and target being root.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.