Imagine exploring a family tree where some children are only children while others have siblings. In this binary tree problem, we need to identify all the lonely nodes - those that are the only child of their parent.
A lonely node is defined as a node that has no siblings (i.e., it's either the only left child or the only right child of its parent). The root node is never considered lonely since it has no parent.
Goal: Given the root of a binary tree, return an array containing the values of all lonely nodes. The order of the result doesn't matter.
Example: In a tree where node 5 has only a left child (node 3), then node 3 is lonely. Similarly, if node 7 has only a right child (node 9), then node 9 is lonely.
Input & Output
Constraints
- The number of nodes in the tree is in the range [1, 1000]
- 1 โค Node.val โค 1000
- Each node value is unique