This was using TreeElement before it was fully defined, which gives undefined
behavior that happened to work with other libraries but not libc++.
Based on patch by Marcus von Appen, modifications for brevity and to ensure we
don't dereference invalid memory. Ref T37477.