过程和代码来自左神的《程序员代码面试指南》,基本相同,只有细微差别。
非常推荐有条件的去购买读一下,很好的一本书。
分别用递归和非递归方式实现二叉树先序、中序和后序遍历
假设节点类如下:
public class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
测评链接:
1.先序遍历 递归
public void preOrderRecur(Node head) {
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
2.中序遍历 递归
public void inOrderRecur(Node head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.value + " ");
inOrderRecur(head.right);
}
3.后序遍历 递归
public void posOrderRecur(Node head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.value + " ");
}
4.先序遍历 非递归
用非递归的方式实现二叉树的先序遍历,具体过程如下:
a、申请一给新的栈,记为stack。然后将头节点head压入stack中
b、从stack中弹出栈顶节点,记为cur,然后打印cur节点的值,再将节点cur的右孩子节点(不为空的话)先压入stack中,最后将cur的左孩子节点(不为空的话)压入stack中。
c、不断重复步骤b,直到stack为空,全部过程结束
public void preOrderUnRecur(Node head) {
System.out.print("pre_order: ");
if (head != null) {
Deque<Node> stack = new ArrayDeque<>();
stack.add(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);
}
}
}
System.out.println();
}
5.中序遍历 非递归
用非递归的方式实现二叉树的中序遍历,具体过程如下:
a、申请一个新的栈,记为stack。初始时,令变量cur=head。
b、先把cur节点压入栈中,对以cur节点为头节点的整棵子树来说,依次把左边界压入栈中,即不停地令cur=cur.left,然后重复步骤b
c、不断重复步骤b,直到发现cur为空,此时从stack中弹出一个节点,记为node。打印node的值,并且让cur=node.right,然后继续重复步骤b。
d、当stack为空且cur为空时,整个过程停止。
public void inOrderUnRecur(Node head) {
System.out.print("in-order: ");
if (head != null) {
Deque<Node> stack = new ArrayDeque<>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
System.out.println();
}
6.后序遍历 非递归
(1)用两个栈实现后序遍历的过程,具体过程如下:
a、申请一个栈,记为s1,然后将头节点head压入s1中
b、从s1中弹出的节点记为cur,然后依次将cur的左孩子节点和右孩子节点压入s1中
c、在整个过程中,每一个从s1中弹出的节点都放进s2中
d、不断重复步骤b和步骤c,直到s1为空,过程停止
e、从s2中依次弹出节点并打印,打印的顺序就是后序遍历的顺序
public void posOrderUnRecur1(Node head) {
System.out.print("pos-order: ");
if (head != null) {
Deque<Node> s1 = new ArrayDeque<>();
Deque<Node> s2 = new ArrayDeque<>();
s1.push(head);
while (!s1.isEmpty()) {
head = s1.pop();
s2.push(head);
if (head.left != null) {
s1.push(head.left);
}
if (head.right != null) {
s1.push(head.right);
}
}
while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}
};
System.out.println();
}
(2)用一个栈实现后序遍历的过程
a、申请一个栈,记为stack,将头节点压入stack,同时设置两个变量h和c。在整个流程中,h代表最近一次弹出并打印的节点,c代表stack的栈顶节点,初始时h为头节点,c为null。
b、每次令c等于当前stack的栈顶节点,但是不从stack中弹出,此时分以下三种情况。
I:如果c的左孩子节点不为null,并且h不等于c的左孩子节点,也不等于c的右孩子节点,则把c的左孩子节点压入stack中。书中对此的解释是:首先h的意义是最近一次弹出并打印的节点,所以,如果h等于c的左孩子节点或者右孩子节点,说明c的左子树与右子树已经打印完毕,此时不应该再将c的左孩子节点放入stack中。否则,说明左子树还没处理过,那么此时将c的左孩子节点压入stack中。
II:如果条件I不成立,并且c的右孩子节点不为null,h不等于c的右孩子节点,则把c的右孩子节点压入stack中。含义是如果h等于c的右孩子节点,说明c的右子树已经打印完毕,此时不应该再将c的右孩子节点放入stack中。否则,说明右子树还没处理过,此时将c的右孩子节点压入stack中。
III:如果条件I和条件II都不成立,说明c的左子树和右子树都已经打印完毕,那么从stack中弹出c并打印,然后令h=c
c、一直重复步骤b,直到stack为空,过程停止
public void posOrderUnRecur2(Node h) {
System.out.print("pos-order: ");
if (h != null) {
Deque<Node> stack = new ArrayDeque<>();
stack.push(h);
Node c = null;
while (!stack.isEmpty()) {
c = stack.peek();
if (c.left != null && h != c.left && h != c.right) {
stack.push(c.left);
} else if (c.right != null && h != c.right) {
stack.push(c.right);
} else {
System.out.print(stack.pop().value + " ");
h = c;
}
}
}
System.out.println();
}