Daily challenge for 2026-06-07
523. Count Nodes With Only One Child
Given an array representation of a binary tree where null represents an empty node, return the number of nodes that have exactly one child.
The array represents the tree in level-order (like LeetCode's tree serialization). For this problem, you can assume the array is given as a list of values where the left child of the node at index i is at index 2*i + 1 and the right child is at index 2*i + 2. A node has exactly one child if one of its children is null and the other is not null.
Example:Input: tree = [1, 2, 3, 4, null, null, 5]Output: 1
Explanation: Node 2 has a left child (4) but no right child. Node 3 has a right child (5) but no left child. Wait, index 1 (value 2) has left child at 3 (value 4) and right child at 4 (value null). So node 2 has exactly one child. Index 2 (value 3) has left child at 5 (value null) and right child at 6 (value 5). So node 3 has exactly one child. Total is 2. Let's correct the example output to 2.