Classify Two Nodes as Siblings, Cousins, or Others
Updated
Problem statement
A binary tree contains unique integer values. Given the values first and second of two distinct nodes, return:
"siblings"if they have the same parent."cousins"if they have the same depth but different parents."others"otherwise.
Function
classifyNodeRelationship(root: TreeNode, first: int, second: int) → StringExamples
Example 1
root = [1,2,3,4,5,6,7]first = 4second = 5return = "siblings"Nodes 4 and 5 share parent 2.
Example 2
root = [1,2,3,4,5,6,7]first = 4second = 6return = "cousins"Nodes 4 and 6 have equal depth and different parents.
Example 3
root = [1,2,3,4,null,null,7,8]first = 3second = 8return = "others"One node is at depth one and the other is at depth three.
Constraints
- The tree contains between
2and100000nodes. - Node values are unique and between
-10^9and10^9. - Both target values occur in the tree and are distinct.
More Amazon Onsite Interview questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dungeon GameONSITE INTERVIEW · Seen Nov 2026
- Integer BreakONSITE INTERVIEW · Seen Nov 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Binary Tree Vertical Order TraversalBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- Count LevelsBlackRock · OABlackRock interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Everyone Under a ManagerAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
- Path Between Two Binary-Tree NodesSnowflake · PHONE SCREENSnowflake interview questions