算法思想:利用栈的先进后出思想实现 先把头结点压栈,定义一个指向栈顶的指针,从栈中取元素并打印,从右向左先判断当前结点有没有右结点,有的话则压栈.再判断结点有么有左节点,有的话就压栈
代码语言:javascript复制 public static void preOrderPrint(Node head){
System.out.print("pre-order: ");
if (head!=null){
Stack<Node> stack=new Stack<>();
stack.push(head);
while (!stack.isEmpty()){
head = stack.pop();
System.out.print(head.value " ");
if (head.right!=null){
stack.push(head.right);
}
if (head.left!=null){
stack.push(head.left);
}
}
}
}