package src;
import java.util.Arrays;
import java.util.LinkedList;
public class MyTree {
private static class TreeNode{
int data;
TreeNode leftChild;
TreeNode rightChild;
TreeNode(int data){
this.data=data;
}
}
public static TreeNode createBinaryTree(LinkedList<Integer> inputList){
TreeNode node=null;
if(inputList == null || inputList.isEmpty())return null;
Integer data=inputList.removeFirst();
if(data != null){
node=new TreeNode(data);
node.leftChild = createBinaryTree(inputList);
node.rightChild=createBinaryTree(inputList);
}
return node;
}
//前序遍历--根左右
public static void preOrderTraveral(TreeNode node){
if(null == node)return;
System.out.println(node.data);
preOrderTraveral(node.leftChild);
preOrderTraveral(node.rightChild);
}
//中序遍历 -- 左根右
public static void inOrderTraveral(TreeNode node){
if(null == node)return;
inOrderTraveral(node.leftChild);
System.out.println(node.data);
inOrderTraveral(node.rightChild);
}
//后序遍历 -- 左右根
public static void postOrderTraveral(TreeNode node){
if(null == node)return;
postOrderTraveral(node.leftChild);
postOrderTraveral(node.rightChild);
System.out.println(node.data);
}
public static void main(String[] args) {
LinkedList<Integer> inputList=new LinkedList<Integer>(
Arrays.asList(new Integer[]{3,2,9,null,null,10,null,null,8,null,4})
);
TreeNode node=createBinaryTree(inputList);
System.out.println("前序遍历:");
preOrderTraveral(node);
System.out.println("中序遍历:");
inOrderTraveral(node);
System.out.println("后序遍历:");
postOrderTraveral(node);
}
}