All Cousins of a Tree Node
Updated
Problem statement
Given the root of a binary tree and the value target of one of its nodes, return the values of that node's cousins.
A cousin is a node on the same level as the target node (the same number of steps from the root) whose parent is different from the target's parent. The target itself and its sibling are not cousins. Return the cousins from left to right as they appear on their level. If there are none (for example when the target is the root), return an empty list.
Function
cousinValues(root: TreeNode, target: int) → List<Integer>Examples
Example 1
root = [11,4,9,2,7,5,13]target = 7return = [5,13]Node 7 is on the third level with parent 4. The other nodes on that level are 2 (also a child of 4, so a sibling, not a cousin), 5 and 13 (children of 9). Answer, left to right: [5,13].
Example 2
root = [10,20,30]target = 20return = []Node 20's only other node on its level is 30, its sibling. Siblings are not cousins, so the answer is empty.
Example 3
root = [8,3,9,1,null,null,4,null,2,null,6]target = 2return = [6]Node 2 is on the fourth level with parent 1; node 6 is on the same level with parent 4. The parents 1 and 4 are not siblings themselves (their parents are 3 and 9), but a cousin only needs the same level and a different parent. Answer: [6].
Constraints
- The tree has between
1and1000nodes. 1 <= node.val <= 10^4, and all node values are differenttargetis the value of a node in the tree
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- Minimum Edits to a Perfect Binary TreeSnowflake · PHONE SCREENSnowflake interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Right Side ViewGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Same Binary TreeOdoo · PHONE SCREENOdoo interview questions
- Binary Tree Zigzag Level Order TraversalLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions