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 ( len ( st ) != 0 and st [ - 1 ] . right == root ) : NEW_LINE INDENT root = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE DEDENT