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.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.
root=tree_node([10, 20, 30]) → []
The root has two children, so the tree has no only children.
root=tree_node([1, None, 2, 3]) → [2, 3]
Node.val ≤ 10⁶root=tree_node([5, 8, 2, None, 9, 4, 6, 3])
[9, 3]
Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.