从我所看到的,你的代码在第二个while循环中存在一些问题.你的想法是正确的方向,但有一些逻辑错误.你拥有的条件是正确的,但不应该在一起,它们应该是分开的.下面的代码实现了您的目标.
public void inOrder(){ // implement this method using non-recursive solution if(m_root==null){ return; } StackmyStack= new Stack (); BSTNode current=m_root; while (current!= null){ myStack.push(current); current=current.getLeft(); } while (!myStack.isEmpty()){ current=(BSTNode)myStack.pop(); System.out.print(current.getInfo()+" "); if(current.getRight() != null){ current=current.getRight(); while (current!= null){ myStack.push(current); current=current.getLeft(); } } } }