As reported: SAP Concur Software Engineer (USA, internal transfer, 2026), virtual technical rounds at LeetCode-medium level: one of the DSA questions was counting the leaf nodes of a binary tree.
Given the root of a binary tree, return the number of leaf nodes — nodes with no left and no right child. An empty tree has 0 leaves.
Example 1
3
/ \
9 20
/ \
15 7
root = [3,9,20,null,null,15,7] -> 3 (9, 15, 7)
Example 2
root = [1] -> 1
Constraints: 0 ≤ number of nodes ≤ 10^5; the tree can be very deep (a chain of thousands of nodes).
Code runner format: one line: the tree in LeetCode's level-order format with null for missing children. Implement countLeaves(root); the harness prints the count.