在写完全二叉树的构建及遍历
public static void main(String[] args) { Node[]nodes=new Node[10]; for(int i=0;i<10;i++){ nodes[i]=new Node(); nodes[i].data=i+""; } for(int i=0;i<nodes.length/2-1;i++){ nodes[i].left=nodes[i*2+1]; nodes[i].right=nodes[i*2+2]; } int index=nodes.length/2-1; nodes[index].left=nodes[index*2+1]; if(nodes.length%2==1){ nodes[index].right=nodes[index*2+2]; } System.out.print("前序遍历:"); fun(nodes[0]); System.out.println(); System.out.print("中序遍历"); fun1(nodes[0]); System.out.println(); System.out.print("后序遍历"); fun2(nodes[0]); } //前序遍历 public static void fun(Node node){ if(node!=null){ System.out.print(node.data+","); fun(node.left); fun(node.right); } } //中序遍历 public static void fun1(Node node){ if(node!=null){ fun(node.left); System.out.print(node.data+","); fun(node.right); } } //后序遍历 public static void fun2(Node node){ if(node!=null){ fun(node.left); fun(node.right); System.out.print(node.data+","); } } } class Node{ String data; Node right; Node left; }