Flatten a Ternary Tree