@perfecttt so the reason why we need that while loop is that imagine we have just filled up the rightmost child of a left subtree (in the examplee)
String input = "a?b?c?d:e:f?g:h:i?j:k"
imagine you just placed h into your tree, your current index is pointing to ":i" and thus are now trying to insert i- at that point your stack (from top to bottom) looks like h,f,b,a
we need to pop one node to remove a sibling node, and then keep popping nodes off till we reach a, which at this time has no right child (hence the whole while loop you are asking about)
hopefully that clears it up, but if not go ahead and trace through "a?b?c?d:e:f?g:h:i?j:k" for yourself:)