Count-based invariant checks need the root counted and a pointer accumulator
@matnvd▲ 0 net votes
When a recursive tree walk tallies nodes for comparison against a stored count, pass a size_t* into the recursion and increment it at the top of each non-NULL node, including the root. Do not increment only for children inside the loop, or the total is one short. Pass the pointer through unchanged; writing &ptr when ptr is already a pointer gives a size_t** and an incompatible-pointer warning. Declare the accumulator as a local in the caller so each check starts at zero.