二叉树的遍历
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c2cc107bc925e2855b77663538d571d.png#pic_center)
1、构造树
public static void main(String[] args) {
TreeNode root = new TreeNode(6);
TreeNode node1_1 = new TreeNode(2);
TreeNode node1_2 = new TreeNode(9);
TreeNode node2_1 = new TreeNode(7);
TreeNode node2_2 = new TreeNode(8);
TreeNode node2_3 = new TreeNode(3);
TreeNode node2_4 = new TreeNode(0);
TreeNode node3_1 = new TreeNode(2);
TreeNode node3_2 = new TreeNode(9);
root.setLeft(node1_1);
root.setRight(node1_2);
node1_1.setLeft(node2_1);
node1_1.setRight(node2_2);
node1_2.setLeft(node2_3);
node1_2.setRight(node2_4);
node2_1.setLeft(node3_1);
node2_1.setRight(node3_2);
System.out.println("*************递归实现*************");
System.out.println("前序遍历------");
Previous_DiGui(root);
System.out.println("\n中序遍历------");
Middle_DiGui(root);
System.out.println("\n后序遍历------");
After_DiGui(root);
System.out.println("\n*************非递归实现*************");
System.out.println("前序遍历------");
Previous_No_DiGui(root);
System.out.println("\n中序遍历------");
Middle_No_DiGui(root);
System.out.println("\n后序遍历------");
After_No_DiGui(root);
}
public static void visit(TreeNode node) {
System.out.print(node.getData() + " ");
}
class TreeNode {
private TreeNode left;
private TreeNode right;
private int data;
public TreeNode(TreeNode left, TreeNode right, int data) {
super();
this.left = left;
this.right = right;
this.data = data;
}
public TreeNode(int data) {
super();
this.data = data;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
2、前序遍历(递归+非递归)
public static void Previous_DiGui(TreeNode node) {
if (node != null) {
visit(node);
Previous_DiGui(node.getLeft());
Previous_DiGui(node.getRight());
}
}
public static void Previous_No_DiGui(TreeNode node) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (node != null || !stack.isEmpty()) {
while (node != null) {
visit(node);
stack.push(node);
node = node.getLeft();
}
if (!stack.isEmpty()) {
node = stack.pop();
node = node.getRight();
}
}
}
3、中序遍历(递归+非递归)
public static void Middle_DiGui(TreeNode node) {
if (node != null) {
Middle_DiGui(node.getLeft());
visit(node);
Middle_DiGui(node.getRight());
}
}
public static void Middle_No_DiGui(TreeNode node) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
if (!stack.isEmpty()) {
node = stack.pop();
visit(node);
node = node.getRight();
}
}
}
4、后序遍历(递归+非递归)
public static void After_DiGui(TreeNode node) {
if (node != null) {
After_DiGui(node.getLeft());
After_DiGui(node.getRight());
visit(node);
}
}
public static void After_No_DiGui(TreeNode node) {
int left = 1;
int right = 2;
Stack<TreeNode> stack1 = new Stack<TreeNode>();
Stack<Integer> stack2 = new Stack<Integer>();
while (node != null || !stack1.empty()) {
while (node != null) {
stack1.push(node);
stack2.push(left);
node = node.getLeft();
}
while (!stack1.empty() && stack2.peek() == right) {
stack2.pop();
visit(stack1.pop());
}
if (!stack1.empty() && stack2.peek() == left) {
stack2.pop();
stack2.push(right);
node = stack1.peek().getRight();
}
}
}
}