递归版本:
public static void reverseIn(Node head){
if (head == null)
return;
reverseIn(head.left);
reverseIn(head.right);
System.out.print(head.value+",");
}
非递归版本:
public static void unReverseIn(Node head){
if (head != null){
Stack<Node> stack1 = new Stack<>();
Stack<Node> stack2 = new Stack<>();
stack1.push(head);
while (!stack1.isEmpty()){
head = stack1.pop();
stack2.push(head);
if (head.left != null)
stack1.push(head.left);
if (head.right != null)
stack1.push(head.right);
}
Node pop = null;
while (!stack2.isEmpty()){
pop = stack2.pop();
System.out.print(pop.value+",");
}
}
}
|