我正在做堆栈和递归。我能够打印出从 1 到 3 的预购订单。我被困在如何进行另一个循环,以便它将从堆栈中弹出所有内容,然后打印后购订单。输出应该类似于
preorder
1
2
3
postorder
3
2
1
public class stack {
public static void iterative(int from, int to) {
for ( ; from <= to; from++ ) {
System.out.print("pre: " + from + "\n"); //This is will print out the preorder
}
//Another loop to print out postorder?
}
public static void main(String[] args) {
stack.iterative(1, 3);
}
非常感谢!
最佳答案
只需从最后一个开始遍历堆栈即可...
for(;to>=from;to--)
关于java - 从Java中的堆栈中打印出后序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726175/