Ctrl K

Only Children

Easy
You are given the root of a binary tree. A node is an only child if its parent has no other child. The root is never an only child, since it has no parent at all. Return the values of all only-child nodes, in any order.
Example 1
root=tree_node([5, 8, 2, None, 9, 4, 6, 3]) → [9, 3]

8 has only a right child, 9, and 9 has only a left child, 3: both are only children. 2 has two children (4 and 6), and 5 is the root with no parent at all.

Example 2
root=tree_node([10, 20, 30]) → []

The root has two children, so the tree has no only children.

Example 3
root=tree_node([1, None, 2, 3]) → [2, 3]
Constraints
  • The tree has between 1 and 1000 nodes
  • 1 ≤ Node.val ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyMirror-Image Tree EasyTree Height EasyNearest Leaf Depth EasyLeaf Path With a Given Total Easy
loading Python…
Input
root=tree_node([5, 8, 2, None, 9, 4, 6, 3])
Expected
[9, 3]

Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.