A binary tree of network switch ids is given in level-order form (a single line of space-separated tokens, null marking a missing child; every non-null node contributes exactly two following tokens for its children).
Count how many nodes have exactly one child (either a left child with no right child, or a right child with no left child). Nodes with zero children or two children do not count.
Line 1: space-separated level-order tokens describing the binary tree (integers and the token null).
A single integer: the number of single-child nodes. If the tree is empty, print 0.
Example 1
Input
1 2 null null 3
Expected
2
Explanation
Root 1 has only a left child (2), so it counts. Node 2 has only a right child (3), so it counts. Node 3 is a leaf and does not count. Total: 2.
Example 2
Input
1 2 3
Expected
0
Explanation
Root 1 has both children 2 and 3 (two children, does not count). Both 2 and 3 are leaves (zero children, do not count). Total single-child nodes: 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →