Cousins in Binary Tree
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given the root of a binary tree whose node values are all unique, and two distinct values x and y that both appear in the tree. Return true if the nodes holding x and y are **cousins**, and false otherwise.
Two nodes are cousins when they are at the same depth but have different parents. The root is at depth 0. Siblings (same parent) are not cousins, and nodes at different depths are not cousins either.
Examples
Constraints
- 2 <= number of nodes <= 1000
- 1 <= Node.val <= 1000
- All values are unique.
- x != y, and both x and y exist in the tree.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.