 <p>You have this problem because you're not correctly using pointers. Look, you create two <code>TreeNode</code>'s on a stack, then you take their addresses, and make them children of new node. But, because they are on stack, they have the same address. So all the left and right children will point to the same location. </p> <p>You probably wanted to allocate them on heap using <code>new</code> or <code>shared_ptr</code>.</p>