package com.gxu.dawnlab_algorithm3;
/**
* 先序、中序、后序的方式递归遍历二叉树
* @author junbin
*
* 2019年6月27日
*/
public class PrintBinaryTree {
public static class Node{
public int data;
public Node left;
public Node right;
public Node(int data){
this.data = data;
}
}
public void preOrderRecur(Node head){
if(head == null){
return;
}
System.out.println(head.data);
preOrderRecur(head.left);
preOrderRecur(head.right);
}
public void inOrderRecur(Node head){
if(head == null){
return;
}
inOrderRecur(head.left);
System.out.println(head.data);
inOrderRecur(head.right);
}
public void posOrderRecur(Node head){
if(head == null){
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.println(head.data);
}
public static void main(String[] args) {
Node head = new Node(1);
head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.right = new Node(6);
head.left.right.left = new Node(7);
head.left.right.right = new Node(8);
// printTree(head);
PrintBinaryTree pbt = new PrintBinaryTree();
//先序遍历
// pbt.preOrderRecur(head);
//中序遍历
pbt.inOrderRecur(head);
//后序遍历
// pbt.posOrderRecur(head);
}
}