二叉树遍历
递归序
public static void f(Node head) {
if (head == null) {
return;
}
f(head.left);
f(head.right);
}
前中后遍历_递归
public static void preOrderRecur(Node head) {
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
public static void inOrderRecur(Node head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.value + " ");
inOrderRecur(head.right);
}
public static void posOrderRecur(Node head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.value + " ");
}
前中后遍历_非递归
前序遍历
用栈模拟系统所维护的栈。
0)、先把头结点放到栈中
1)、每次弹出一个结点,记为cur
2)、打印cur
3)、如果cur有右、左