If the popped node is right child of top , then remove the top as well . Left child of the top must have processed before .
while ( ! st . empty ( ) && st . top ( ) -> right == root ) { root = st . top ( ) ; st . pop ( ) ; } }