public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
/**
* 创建二叉树
* @param root
*/
public void createTree(TreeNode root){
Scanner scan=new Scanner(System.in);
int val=scan.nextInt();
if(-1==val)
root=null;
else{
root = new TreeNode(val);
createTree(root.left);
createTree(root.right);
}
}
/**
* 删除二叉树
* @param root
*/
public void deleteTree(TreeNode root){
if(root!=null){
deleteTree(root.left);
deleteTree(root.right);
root=null;
}
}
/**
* 二叉树的先序遍历
* @param T
*/
public void PreOrder(TreeNode T){
if(T!=null){
System.out.println(T.val);
PreOrder(T.left);
PreOrder(T.right);
}
}
/**
* 二叉树的层次遍历
* @param T
*/
public void LevelOrder(TreeNode T){
Queue<TreeNode> q=new LinkedList<TreeNode>();//Queue是个接口,不能实例化,而LinkedList实现了Queue接口,所以可以把LinkedList当队列使用
if(T!=null)
q.offer(T);
while(!q.isEmpty()){
TreeNode tempNode=q.element();
System.out.println(tempNode.val);
if(tempNode.left !=null)
q.offer(tempNode.left);
if(tempNode.right!=null)
q.offer(tempNode.right);
q.poll();
}
}