1004 Build tree by recording parent information and compute level by tracking parent up to the root.